Move Pieces to obtain a string - Easy Solution - Nice Explanation - 🌟 Beats 100% 🌟
Intuition\n\n1. target string and the start string should be of same length (contains same number of characters).\n\n1. Using two pointer approach, iterate all 
# Intuition\n\n1. target string and the start string should be of same length (contains same number of characters).\n\n1. Using two pointer approach, iterate all the elements of the string.It requires less time complexity if working character array than string.Hence string is converted into character array using toCharArray().\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n**Initialization**:\n\nInitialize n to be the length of the string start or the string target.\n\n    int n=start.length();\n\nConvert start string to character array and store it in st character array and same for target string to tar character array.\n\n    char[] st=start.toCharArray();\n    char[] tar=target.toCharArray();\n\nInitialize i and j to be 0, the pointer to keep track of st and tar character array.\n\n    int i=0,j=0;\n\n**Checking possibility of the conversion**:\n\nIterate through all the characters of the st and the tar character array.\n\n    while(i<=n && j<=n){\n\nIncrement i pointer till we encounter the character as _ and i is less than n.\n\n    while(i<n && tar[i]==\'_\')\n        i++;\n\nIncrement j pointer till we encounter the character as _ and j is less than n.\n\n    while(j<n && st[j]==\'_\')\n        j++;\n\nIf i and j reaches the end of the string, then the string contains only _, So return true .If either i or j reaches the end of the array, then return false.\n\n    if(i==n || j==n)\n    {\n        return i==n && j==n;\n    }\n\nIf st[j] and tar[i] is not equal, then return false immediately.\n\n    if(st[j]!=tar[i])\n        return false;\n\ncheck if character at index i of tar array is \'L\', then if j<i -> L is to left of _ in the st character array.We cannot move it to right in the tar array since blankspace is at right, so return false.\n\n    if(tar[i]==\'L\')\n    {\n        if(j<i)\n            return false;\n    }\n\nOtherwise, character at index i of the tar array will be \'R\', then if i<j -> R is to right of _ in the st character array.We cannot move it to right since the blankspace is at left, so return false. \n\n    else\n    {\n        if(i<j)\n            return false;\n    }\n\nIncrement i and j pointer at the end of each iteration of the outer loop.\n\n    i++;\n    j++;\n\n**Returning value**:\n\nIf the loop doesn\'t terminate by return condition, then string start can be converted into string target by making those moves.Hence return true.\n\n    return true;\n\n<!-- Describe your approach to solving the problem. -->\n# Complexity\n- ***Time complexity***: **O(n)**.\n      n -> number of the characters in the start or target string.\n\n**Outer Loop**: **O(n)**.\n\nThe outer while loop runs as long as **i and j** are within the bounds of the **string lengths**.\n\nThe total number of iterations of this loop depends on the number of characters in the strings. Thus, **at most O(n) iterations** are made in the outer loop.\n\n**Inner loop**: **O(n)**.\n\nInside the outer loop, there are two while loops:\n\n**First inner loop**: \n\nThis loop skips **underscores (_)** in the target string.\n\nEach character in target is **processed exactly once**, either by this loop (if it\u2019s _) or by the outer loop (if it\u2019s L or R).\n\n**Second inner loop**:\n\nSimilarly, this loop skips underscores (_) in the start string.\n\nEach character in start is processed exactly once.\n\n**Overall Time Complexity**: **O(n)**.\n\nSince each character is processed **exactly once** in either the outer loop or one of the inner loops, the **combined work done** by the inner and outer loops for all iterations is **O(n)**.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- ***Space complexity***: **O(1)**.\n    The program uses **constant amount of time**.\n\n**Input string**: **O(1)**.\n\nThe function does not create any additional data structures to store the strings. It processes the characters directly using the **indices i and j**.\n\n**Auxillary Space**: **O(1)**.\n\nThe program uses a **constant amount of extra space** to store variables like i, j, and the character arrays **st and tar**. \n\nThese arrays are references to the input strings, not additional copies of the data.\n\n**Overall Space complexity**: **O(1)**.\n\nThe space complexity is **O(1) as no additional space** proportional to the input size is used.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public boolean canChange(String start, String target) {\n        //2 strings : start and target : length n\n        //Each string : \'L\',\'R\' and \'_\'\n        // start="_L__R__R_" target="L______RR"\n\n        int n=start.length();\n        //same length for start and target string.\n\n       char[] st=start.toCharArray();\n       char[] tar=target.toCharArray();\n\n        int i=0,j=0;\n\n        while(i<=n && j<=n)\n        {\n            while(i<n && tar[i]==\'_\')\n              i++;\n            while(j<n && st[j]==\'_\')\n              j++;\n            if(i==n || j==n)\n            {\n                return i==n && j==n;\n            }\n\n            if(st[j]!=tar[i])\n              return false;\n            if(tar[i]==\'L\')\n            {\n                if(j<i)\n                  return false;\n            }\n            else\n            {\n                if(i<j)\n                  return false;\n            }\n            i++;\n            j++;\n        }\n        return true;\n\n    }\n}\n```
['Two Pointers', 'String', 'Java']