<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 {
public:
    string pushDominoes(string dominoes) {
        // We will determine the resulting string by determining the distance of each '.' in
        // dominoes from the nearest 'L' and 'R'
        // A '.' with no 'L' to its right and no 'R' to its left remains standing
        // A '.' with equidistant 'L' and 'R' remains standing
        // A '.' closer to a 'L' falls left
        // A '.' closer to a 'R' fallse right

        // Keep track of whether 'L' or 'R' was previously seen
        char last = '.';

        // Keep track of the distance from the nearest 'L' or 'R'
        int dist = 0;

        // Initialize a vector of all distances of each index from the closes 'R'
        vector<int> right(dominoes.length(), 0);
        for (int i = 0; i < dominoes.length(); i++) {
            // Reset dist if the current domino is not a '.'
            if (dominoes[i] != '.') {
                last = dominoes[i];
                dist = 0;
            }

            // Otherwise if the last seen is an 'R'
            else if (dominoes[i] == '.' && last == 'R') {
                dist++;
                right[i] = dist;
            }
        }

        // Initialize a vector of all distances of each index from the closest 'L'
        last = '.';
        dist = 0;
        vector<int> left(dominoes.length(), 0);
        for (int i = dominoes.length() - 1; i >= 0; i--) {
            // Reset dist if the current domino is not a '.'
            if (dominoes[i] != '.') {
                last = dominoes[i];
                dist = 0;
            }

            // Otherwise if the last seen is an 'L'
            else if (dominoes[i] == '.' && last == 'L') {
                dist++;
                left[i] = dist;
            }
        }

        // Traverse dominoes
        for (int i = 0; i < dominoes.length(); i++) {
            // Skip 'L' and 'R'
            if (dominoes[i] == 'L' || dominoes[i] == 'R') {
                continue;
            }

            // Skip '.' that have no 'L' to its right or 'R' to its left
            if (!(left[i] || right[i])) {
                continue;
            }

            // Skip '.' that have equidistant 'L' and 'R'
            if (left[i] == right[i]) {
                continue;
            }

            if (left[i] == 0) {
                dominoes[i] = 'R';
            } else if (right[i] == 0) {
                dominoes[i] = 'L';
            }

            // Set '.' according to the closer 'L' or 'R'
            else if (left[i] < right[i]) {
                dominoes[i] = 'L';
            } else if (right[i] < left[i]) {
                dominoes[i] = 'R';
            }
        }

        return dominoes;
    }
};
21843