838. Push Dominoes LeetCode Solution
In this guide, you will get 838. Push Dominoes LeetCode Solution with the best time and space complexity. The solution to Push Dominoes problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Push Dominoes solution in C++
- Push Dominoes solution in Java
- Push Dominoes solution in Python
- Additional Resources

Problem Statement of Push Dominoes
There are n dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.
After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
You are given a string dominoes representing the initial state where:
dominoes[i] = ‘L’, if the ith domino has been pushed to the left,
dominoes[i] = ‘R’, if the ith domino has been pushed to the right, and
dominoes[i] = ‘.’, if the ith domino has not been pushed.
Return a string representing the final state.
Example 1:
Input: dominoes = “RR.L”
Output: “RR.L”
Explanation: The first domino expends no additional force on the second domino.
Example 2:
Input: dominoes = “.L.R…LR..L..”
Output: “LL.RR.LLRRLL..”
Constraints:
n == dominoes.length
1 <= n <= 105
dominoes[i] is either 'L', 'R', or '.'.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
838. Push Dominoes LeetCode Solution in C++
class Solution {
public:
string pushDominoes(string dominoes) {
int L = -1;
int R = -1;
for (int i = 0; i <= dominoes.length(); ++i)
if (i == dominoes.length() || dominoes[i] == 'R') {
if (L < R)
while (R < i)
dominoes[R++] = 'R';
R = i;
} else if (dominoes[i] == 'L') {
if (R < L || L == -1 && R == -1) {
if (L == -1 && R == -1)
++L;
while (L < i)
dominoes[L++] = 'L';
} else {
int l = R + 1;
int r = i - 1;
while (l < r) {
dominoes[l++] = 'R';
dominoes[r--] = 'L';
}
}
L = i;
}
return dominoes;
}
};
/* code provided by PROGIEZ */
838. Push Dominoes LeetCode Solution in Java
class Solution {
public String pushDominoes(String dominoes) {
char[] s = dominoes.toCharArray();
int L = -1;
int R = -1;
for (int i = 0; i <= dominoes.length(); ++i)
if (i == dominoes.length() || s[i] == 'R') {
if (L < R)
while (R < i)
s[R++] = 'R';
R = i;
} else if (s[i] == 'L') {
if (R < L || L == -1 && R == -1) {
if (L == -1 && R == -1)
++L;
while (L < i)
s[L++] = 'L';
} else {
int l = R + 1;
int r = i - 1;
while (l < r) {
s[l++] = 'R';
s[r--] = 'L';
}
}
L = i;
}
return new String(s);
}
}
// code provided by PROGIEZ
838. Push Dominoes LeetCode Solution in Python
class Solution:
def pushDominoes(self, dominoes: str) -> str:
ans = list(dominoes)
L = -1
R = -1
for i in range(len(dominoes) + 1):
if i == len(dominoes) or dominoes[i] == 'R':
if L < R:
while R < i:
ans[R] = 'R'
R += 1
R = i
elif dominoes[i] == 'L':
if R < L or (L, R) == (-1, -1):
if (L, R) == (-1, -1):
L += 1
while L < i:
ans[L] = 'L'
L += 1
else:
l = R + 1
r = i - 1
while l < r:
ans[l] = 'R'
ans[r] = 'L'
l += 1
r -= 1
L = i
return ''.join(ans)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.