C++ | Explanation |  Beginner Friendly | Easy-Understanding
Problem states that we need to convert start string to target string by moving \'L\' to left and \'R\' to right in Start.\n\n3 observations to make here - \n\n1
**Problem** states that we need to convert **start** string to **target** string by moving \'L\' to left and \'R\' to right in **Start**.\n\n**3 observations to make here** - \n\n**1. Start string can only be converted into Target string if and only if relative order of \'L\' and \'R\' is equal.**\n\nExample - \nStart = "-L-R--L-R"\nTarget = "--R--L--LR"\n\nNow here we can see that order of LR in start is -> LRLR but in Target is RLLR so it can never be converted into Target because L and R can never cross each other.\n\n\n**2. Now relative order being same doesn\'t concludes that Start can always be converted to the Target.**\n\nExample - \nStart = "L-RRL"\nTarget = "LR-RL"\n\nHere relative order is same but to convert Start to Target we will need to move R to Left which is not Possible.\n\nNow here comes our 3rd Observation\n\n**3. That although relative order is same but if need to move any R of start to Left or any L to right then also Start can never be converted into Target.**\n\nNow how to check for this??\n\nTo check (suppose for R in the above example) this we will store indexes of R from both Start and Target in say arrays - r1 (For start) and r2 for(Target) and check for every i in r1  r1[i]<=r2[i].\nSimilarly for L l1[i]>=l2[i]\n\n\n**DRY RUN -**\n\nStart = "L - R R L"\nTarget="L R - R L"\n\n**now here relative order is same** , so may be start can be converted to Target\n\nnow r1 and r2 will look like this - \n\nr1 = [2,3]  r2 = [1,3]\n\nso now for i=0 r1[i]>r2[i] which simply denotes that to convert the S->T we need to move R at index 2 in Start in Left direction to make it match with R at index 1 in Target and which is not possible , Hence we cannot convert S->T.\n\n\nCode - \n\n```\nclass Solution {\npublic:\n    bool static seqSame(string s1,string s2){\n        string a,b;\n        for(auto it : s1) if(it!=\'_\') a+=it;\n        for(auto it : s2) if(it!=\'_\') b+=it;\n        return a==b;\n    }\n    bool canChange(string start, string target) {\n        int n = start.length();\n        if(!seqSame(start,target)) return false;\n        vector<int> l1,l2,r1,r2;\n        \n        for(int i=0 ; i<n ; i++){\n            if(start[i]==\'L\') l1.push_back(i);\n            if(start[i]==\'R\') r1.push_back(i);\n            if(target[i]==\'L\') l2.push_back(i);\n            if(target[i]==\'R\') r2.push_back(i);\n        }\n        \n        for(int i=0 ; i<l1.size() ; i++) if(l1[i]<l2[i]) return false;\n        for(int i=0 ; i<r1.size() ; i++) if(r1[i]>r2[i]) return false;\n        \n        return true;\n    }\n};\n```\n\nJust in Case you like the approach and effort , **DO UPVOTE.**\n
['C']