There are
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[i] = 'L' , if theith domino has been pushed to the left, -
dominoes[i] = 'R' , if theith domino has been pushed to the right, and -
dominoes[i] = '.' , if theith domino has not been pushed.
Return a string representing the final state.
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.
Output: "LL.RR.LLRRLL.."
Explanation: below picture representing the input string


Constraints:
n == dominoes.length 1 <= n <= 105 dominoes[i] is either'L' ,'R' , or'.' .
Contents
Solution 1 - Using two pass technique, calculate the net force at each position
Let's understand one thing from the problem, if the domino at position
At each second, the domino is falling on left side is pushing other vertical dominoes towards left and the domino falling on right side
is pushing other vertical dominoes towards right.
If there is a vertical domino in between a
In this approach, we will use a temporary array to calculate forces at each index, i.e. one time towards right direction, compute the affect of domonies
falling to right side
implementation details:
-
We will first initialize a temporary to store the forces, call it
forces , and another variableforce to keep track of net force at current index. -
Then, will loop through each domino from left to right and do the following:
-
If the domino is
'R' then, setforce ton (the length of 'dominoes' input string, because the maximum number of domonies that a R can fall on are n-1 other dominoes if the current domino is R). -
If the domino is
'L' then, setforce to0 (to break that force as this would fall onto left side). -
If the domino is
'.' then, setforce toMAX(force-1, 0) (reason for this is we want to see whether force continues to decrement, that is if R continues to fall without any 'L' in between). -
Update
forces array at current index with theforce value computed from above 3 conditions.
By the end of this loop, forces array will have either 0 or positive numbers, after computing net forces if the result remains positive, that means it is going to be 'R' domino. -
If the domino is
-
Now, reset
force = 0 and loop through each domino from right to left and do the following:-
If the domino is
'L' then, setforce ton . -
If the domino is
'R' then, setforce to0 . -
If the domino is
'.' then, setforce toMAX(force-1, 0) (reason for this is we want to see whether force continues to decrement, that is if R continues to fall without any 'L' in between). -
Update
forces array at current index with the-force value computed from above 3 conditions. The reason we are subtracting withforce is to get the net force value.
-
If the domino is
-
Finally, loop through
forces array, and if the value is less than 0, then that is a left domino 'L', if the value is greater than 0, then that is a right domino and if the value is 0, then it is a vertical domino. .
Example:
Consider an example with
-
After first pass from left to right, forces array will look like this,
[0, 0, 0, 14, 13, 12, 11, 0, 14, 13, 12, 0, 0, 0] . -
After second pass from right to left, forces array will look like this,
[-13, -14, 0, 14, 2, 0, -2, -14, 14, 1, -1, -14, 0, 0] . -
So, the final state of dominoes will be,
LL.RR.LLRRLL.. .
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n), since we are using a temporary array to store the net forces.
Above implementations source code can be found at