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:

Return a string representing the final state.

Input: dominoes = "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.
Input: dominoes = ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."
Explanation: below picture representing the input string
Example 2a below picture representing the output after Dominoes pushed to left and right sides.
Example 2b
Constraints:

Contents

Let's understand one thing from the problem, if the domino at position i is 'L' it falls on to the neighbouring dominoes on left side, if it is 'R' then it falls on to the neighbouring dominoes on right side, and if it is '.' then it is not falling on either side.

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 'L' and 'R' that stays vertical. This is an important piece of information for the below approach, if there is one vertical domino between L and R, for example R.L then it stays as is. But, if there are odd number of vertical dominoes, for example R...L, centered domino stays vertical, R will fall to the right, and L will fall to the left.

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 'R', and second time towards left direction, compute the affect of dominoes falling to left side 'R', this will give us the net affect of both L and R are falling at the same time.

implementation details:
Example:

Consider an example with dominoes = ".L.R...LR..L..".

public class PushDominoes { static String pushDominoes(String dominoes) { char[] dominoesArray = dominoes.toCharArray(); int n = dominoesArray.length; int[] forces = new int[n]; int force = 0; for(int i=0; i<n; i++) { // going from left to right, towards right, meaning R falls on neighbour dominoes if(dominoesArray[i] == 'R') { force = n; } else if(dominoesArray[i] == 'L') { force = 0; } else { force = Math.max(force-1, 0); } forces[i] += force; } force = 0; for(int i=n-1; i>=0; i--) { if(dominoesArray[i] == 'L') { force = n; } else if (dominoesArray[i] == 'R') { force = 0; } else { force = Math.max(force-1, 0); } forces[i] -= force; } StringBuilder sb = new StringBuilder(); for(int i=0;i<n; i++) { if(forces[i] <0) { sb.append('L'); } else if (forces[i]>0) { sb.append('R'); } else { sb.append('.'); } } return sb.toString(); } public static void main(String[] args) { System.out.println(pushDominoes(".L.R...LR..L..")); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of dominoes input string.
Space complexity: O(n), since we are using a temporary array to store the net forces.

Above implementations source code can be found at GitHub link for Java code