<p>You are given two strings <code>start</code> and <code>target</code>, both of length <code>n</code>. Each string consists <strong>only</strong> of the characters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;_&#39;</code> where:</p>

<ul>
	<li>The characters <code>&#39;L&#39;</code> and <code>&#39;R&#39;</code> represent pieces, where a piece <code>&#39;L&#39;</code> can move to the <strong>left</strong> only if there is a <strong>blank</strong> space directly to its left, and a piece <code>&#39;R&#39;</code> can move to the <strong>right</strong> only if there is a <strong>blank</strong> space directly to its right.</li>
	<li>The character <code>&#39;_&#39;</code> represents a blank space that can be occupied by <strong>any</strong> of the <code>&#39;L&#39;</code> or <code>&#39;R&#39;</code> pieces.</li>
</ul>

<p>Return <code>true</code> <em>if it is possible to obtain the string</em> <code>target</code><em> by moving the pieces of the string </em><code>start</code><em> <strong>any</strong> number of times</em>. Otherwise, return <code>false</code>.</p>

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

<pre>
<strong>Input:</strong> start = &quot;_L__R__R_&quot;, target = &quot;L______RR&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> We can obtain the string target from start by doing the following moves:
- Move the first piece one step to the left, start becomes equal to &quot;<strong>L</strong>___R__R_&quot;.
- Move the last piece one step to the right, start becomes equal to &quot;L___R___<strong>R</strong>&quot;.
- Move the second piece three steps to the right, start becomes equal to &quot;L______<strong>R</strong>R&quot;.
Since it is possible to get the string target from start, we return true.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> start = &quot;R_L_&quot;, target = &quot;__LR&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> The &#39;R&#39; piece in the string start can move one step to the right to obtain &quot;_<strong>R</strong>L_&quot;.
After that, no pieces can move anymore, so it is impossible to obtain the string target from start.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> start = &quot;_R&quot;, target = &quot;R_&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.</pre>

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

<ul>
	<li><code>n == start.length == target.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>start</code> and <code>target</code> consist of the characters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;_&#39;</code>.</li>
</ul>

"""

Brute
1. count char count for start and target if not equal return False
2. the order of L and R should be same in both the strings
3. the L char can only appear right to same L in target and R char can only appear left to same R in target

TC : O(3N)

Optimal
Iterate over while loop and skip blank spaces
if reached end or differenct chars in both break
if viloating the L char can only appear right to same L in target and R char can only appear left to same R in target
return False

increase i and j

TC : O(N)




"""

class Solution:
    def canChange(self, s: str, t: str) -> bool:

        i = 0
        j = 0
        n = len(s)

        while i < n or j < n:

            while i < n and s[i] == '_':
                i += 1
            
            while j < n and t[j] == '_':
                j += 1
        
            if i == n or j == n or s[i] != t[j]:

                break # ensure order of L and R should be same
            
            if ( s[i] == 'L' and i < j ) or ( s[i] == 'R' and i > j):
 
                return False 
                # L in start should only appear right to the target not before
                # R in start should only appear left  to the target not after
                
            i += 1
            j += 1

        return i == n and j == n # if there length or count of chars is different



        """

        Intutive and logical Base Solution

        s_count = {
            'L' : 0,
            'R' : 0,
            '_' : 0
        }

        t_count = {
            'L' : 0,
            'R' : 0,
            '_' : 0
        }

        n = len(s)
        if n != len(t):
            return False

        start = []
        target = []
        
        for i in range(n):

            t_char = t[i]
            s_char = s[i]

            if s_char != '_':
                start.append((s_char, i))
            
            if t_char != '_':
                target.append((t_char, i))

            s_count[s_char] += 1
            t_count[t_char] += 1
    

        for key, values in s_count.items():

            if values != t_count[key]:
                return False # the count of L or R or _ is differenct in both

        for i in range(len(start)):

            s_char, s_index = start[i]
            t_char, t_index = target[i]

            if s_char != t_char:
                return False # meaning the order of L and R is not same in both the strings
            
            # 3rd condition 
            # L in start should only appear right to the target not before
            # R in start should only appear left  to the target not after
            if s_char == "L" and s_index < t_index:
                    return False
            
            if t_char == "R" and s_index > t_index:
                return False
        
        return True
        """

        





        
153