<p>There are <code>n</code> 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.</p>

<p>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.</p>

<p>When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.</p>

<p>For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.</p>

<p>You are given a string <code>dominoes</code> representing the initial state where:</p>

<ul>
	<li><code>dominoes[i] = &#39;L&#39;</code>, if the <code>i<sup>th</sup></code> domino has been pushed to the left,</li>
	<li><code>dominoes[i] = &#39;R&#39;</code>, if the <code>i<sup>th</sup></code> domino has been pushed to the right, and</li>
	<li><code>dominoes[i] = &#39;.&#39;</code>, if the <code>i<sup>th</sup></code> domino has not been pushed.</li>
</ul>

<p>Return <em>a string representing the final state</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> dominoes = &quot;RR.L&quot;
<strong>Output:</strong> &quot;RR.L&quot;
<strong>Explanation:</strong> The first domino expends no additional force on the second domino.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/05/18/domino.png" style="height: 196px; width: 512px;" />
<pre>
<strong>Input:</strong> dominoes = &quot;.L.R...LR..L..&quot;
<strong>Output:</strong> &quot;LL.RR.LLRRLL..&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == dominoes.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>dominoes[i]</code> is either <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, or <code>&#39;.&#39;</code>.</li>
</ul>

class Solution:
    def pushDominoes(self, dominoes: str) -> str:
        # At time 0 we SIMULTANEOUSLY push some of the dominoes to the left or right
        # At each second, the falling dominoes push its neighbors to fall in the same direction
        # if a domino has a neighbor pushing it to the left and a neighbor pushing it to the right then it remains standing
            # This is because both dominoes balance the forces out
        
        # IN: string<LR.> where L means pushed to the left, R pushed to the right and . standing
        # OUT: string representing the final state

        # ex: "RR.L" at time 0
        # "RR.L" at time 1. The . didn't become a L or R because it had a L and R neighbor trying to push it at the same time

        # ex: ".L.R...LR..L.." at t=0
        #     "LL.RR.LLRRLL.." at t=1
        #     "LL.RR.LLRRLL.." at t=2 no further changes

        # ex: "R..."
        #     "RR.." t = 1
        #     "RRR." t = 2
        #     "RRRR" t = 4

        # ex: RL -> RL

        # Find all L and R chars
        # Determine the range of chars that each L and R will cover
        # ".L.R...LR..L..
        # list = [(1, L), (3, R), (3, L), (2, R), (2, L)]
        # AFTER 1 second we will process all 5 starting points
            # Process the (1, L) -> LL + .R...LR..L.
            # Process the (3, R) -> RR + LL. + ..LR..LL -> list = [(2, R), (3, L), (2, R), (2, L)]
            # Process the (3, L) -> LL + LL.RR. + R..L.. -> list = [(2, R), (2, L), (2, R), (2, L)]
            # Process the (2, R) -> RR -> LL.RR.LLRR.L.. list = [(2, R), (2, L), (1, R), (2, L)]
            # Process the (2, L) -> LL -> LL.RR.LL.RRLL.. list = [(2, R), (2, L), (1, R), (1, L)]

        # q=[(0, R), (1, R), (2, L)]

        queue = collections.deque()
        dominoes_list = list(dominoes)
        left_index_to_skip = set()

        # Populate the deque with the starting indexes of the starting L and R
        for i, char in enumerate(dominoes):
            if char != ".":
                queue.append((i, char))

        while queue:
            i, char = queue.popleft()

            if char == "L" and i not in left_index_to_skip:
                if i - 1 >= 0 and dominoes_list[i - 1] == ".":
                    dominoes_list[i - 1] = "L"
                    queue.append((i - 1, "L"))

            elif char == "R":
                if i + 2 < len(dominoes_list) and dominoes_list[i + 2] == "L":
                    left_index_to_skip.add(i + 2)
                    continue
                elif i + 1 < len(dominoes_list) and dominoes_list[i + 1] == ".":
                    dominoes_list[i + 1] = "R"
                    queue.append((i + 1, "R"))    
        return "".join(dominoes_list)
        # .L.R...LR..L..
        # LL.RR.LLRRLL..
        # |   | |   |
24380