<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:
        # n dominoes
        #simultaneously push some dominoes left or right
        #each second, each dominoe that is falling to the left pushes the adjacent to the left
        #given dominoes that fell left or right, return the final state

        """
        Input: dominoes = "RR.L"

        / / | \   


        dominoes = "LL.R...LR..L.."       queue = (1,L), (3,R), (7,L), (8,R), (11,L)
                     while queue
                         old queue = queue
                         for every pos, dir in old queue
                                 if dir == L and pos - 1 is untouched and pos -2 != R
                                        pos -1 = L
                                        queue.append(pos-1, L)
                                  if dir == R and pos + 1 is untouched and pos+2 != L
                                        pos+1 = R
                                        queue.append(pos+1, R)
                          old_queue = queue
                             
                      

        #just care about non pushed dominoes

        #For every non pushed dominoe (assuming both adj domines) and just accounting for the old states
 
        #Case 1: if both adj are falling in different directions, the dominoe stays unpush
        #Case 2 : right dominoe is falling left then we mark dominoe as L, incr a counter
        #Case 3: left dominoe is falling right them we mark dominoe as R    incr counter

        if number of states is 0
            return our states
        set our  states to the updated state repeat the algorithm

        ..........................LLLL

        creating a new state will take O(n)

        will repeat a max of n times

        O(n^2)

         


        """

        queue = []
        n = len(dominoes)
        for i, direction in enumerate(dominoes):
            if direction != ".":
                queue.append((i, direction))
        dominoesArr = list(dominoes)
        while queue:
            temp = dominoesArr.copy()
            for _ in range(len(queue)):
                pos, direction = queue.pop(0)
                if  direction == "L" and pos -1 >= 0 and dominoesArr[pos-1] == "." and (pos - 2 < 0 or dominoesArr[pos-2] != "R"):
                    temp[pos-1] = "L"
                    queue.append((pos-1, direction))
                elif  direction == "R" and pos + 1 < n and dominoesArr[pos+1] == "." and (pos + 2 >= n  or dominoesArr[pos+2] != "L"):
                    temp[pos+1] = "R"
                    queue.append((pos+1, direction))
            dominoesArr = temp
           
        return "".join(dominoesArr)

25700