✅ 100% Beats | Short & Simple
\n\n\n\n# Two Pointers (Mostly 100% [1][2][3][4][5][6][7][8][9][10])\npython3 []\nclass Solution:\n    def canChange(self, start: str, target: str) -> bool:\n  
![image.png](https://assets.leetcode.com/users/images/5e874709-a7f9-4d52-9fd5-af8de7f4f7d6_1733364256.517872.png)\n\n\n\n# Two Pointers (Mostly 100% [[1]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470618274)[[2]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470617908)[[3]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470616981)[[4]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470616762)[[5]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470616452)[[6]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470620484)[[7]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470621041)[[8]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470621659)[[9]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470616312)[[10]](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470622222))\n```python3 []\nclass Solution:\n    def canChange(self, start: str, target: str) -> bool:\n        if start == target:\n            return True\n        waitL = 0   \n        waitR = 0    \n\n        for curr, goal in zip(start, target):\n            if curr == \'R\':\n                if waitL > 0:\n                    return False\n                waitR += 1  \n            if goal == \'L\':\n                if waitR > 0:\n                    return False\n                waitL += 1\n            if goal == \'R\':\n                if waitR == 0:\n                    return False\n                waitR -= 1 \n            if curr == \'L\':\n                if waitL == 0:\n                    return False\n                waitL -= 1    \n        return waitL == 0 and waitR == 0\n```\n```cpp []\nclass Solution {\npublic:\n    bool canChange(string start, string target) {\n        if (start == target) {\n            return true;\n        }\n        int waitL = 0;\n        int waitR = 0;\n        \n        for (int i = 0; i < start.length(); i++) {\n            char curr = start[i];\n            char goal = target[i];\n            if (curr == \'R\') {\n                if (waitL > 0) {\n                    return false;\n                }\n                waitR++;\n            }\n            if (goal == \'L\') {\n                if (waitR > 0) {\n                    return false;\n                }\n                waitL++;\n            }\n            if (goal == \'R\') {\n                if (waitR == 0) {\n                    return false;\n                }\n                waitR--;\n            }\n            if (curr == \'L\') {\n                if (waitL == 0) {\n                    return false;\n                }\n                waitL--;\n            }\n        }\n        return waitL == 0 && waitR == 0;\n    }\n};\n```\n```java []\nclass Solution {\n    public boolean canChange(String start, String target) {\n        if (start.equals(target)) {\n            return true;\n        }\n        int waitL = 0;\n        int waitR = 0;\n        \n        for (int i = 0; i < start.length(); i++) {\n            char curr = start.charAt(i);\n            char goal = target.charAt(i);\n            \n            if (curr == \'R\') {\n                if (waitL > 0) {\n                    return false;\n                }\n                waitR++;\n            }\n            if (goal == \'L\') {\n                if (waitR > 0) {\n                    return false;\n                }\n                waitL++;\n            }\n            if (goal == \'R\') {\n                if (waitR == 0) {\n                    return false;\n                }\n                waitR--;\n            }\n            if (curr == \'L\') {\n                if (waitL == 0) {\n                    return false;\n                }\n                waitL--;\n            }\n        }\n        \n        return waitL == 0 && waitR == 0;\n    }\n}\n```\n```csharp []\npublic class Solution {\n    public bool CanChange(string start, string target) {\n        if (start == target) return true;\n        int waitL = 0, waitR = 0;\n        \n        for (int i = 0; i < start.Length; i++) {\n            char curr = start[i];\n            char goal = target[i];\n            \n            if (curr == \'R\') {\n                if (waitL > 0) return false;\n                waitR++;\n            }\n            if (goal == \'L\') {\n                if (waitR > 0) return false;\n                waitL++;\n            }\n            if (goal == \'R\') {\n                if (waitR == 0) return false;\n                waitR--;\n            }\n            if (curr == \'L\') {\n                if (waitL == 0) return false;\n                waitL--;\n            }\n        }\n        return waitL == 0 && waitR == 0;\n    }\n}\n```\n```golang []\nfunc canChange(start string, target string) bool {\n    if start == target {\n        return true\n    }\n    waitL, waitR := 0, 0\n    \n    for i := 0; i < len(start); i++ {\n        curr := start[i]\n        goal := target[i]\n        \n        if curr == \'R\' {\n            if waitL > 0 {\n                return false\n            }\n            waitR++\n        }\n        if goal == \'L\' {\n            if waitR > 0 {\n                return false\n            }\n            waitL++\n        }\n        if goal == \'R\' {\n            if waitR == 0 {\n                return false\n            }\n            waitR--\n        }\n        if curr == \'L\' {\n            if waitL == 0 {\n                return false\n            }\n            waitL--\n        }\n    }\n    return waitL == 0 && waitR == 0\n}\n```\n```swift []\nclass Solution {\n    func canChange(_ start: String, _ target: String) -> Bool {\n        if start == target { return true }\n        var waitL = 0, waitR = 0\n        \n        let startChars = Array(start)\n        let targetChars = Array(target)\n        \n        for i in 0..<startChars.count {\n            let curr = startChars[i]\n            let goal = targetChars[i]\n            \n            if curr == "R" {\n                if waitL > 0 { return false }\n                waitR += 1\n            }\n            if goal == "L" {\n                if waitR > 0 { return false }\n                waitL += 1\n            }\n            if goal == "R" {\n                if waitR == 0 { return false }\n                waitR -= 1\n            }\n            if curr == "L" {\n                if waitL == 0 { return false }\n                waitL -= 1\n            }\n        }\n        return waitL == 0 && waitR == 0\n    }\n}\n```\n```javascript [JS]\n// JavaScript\n\nvar canChange = function(start, target) {\n    if (start === target) return true;\n    let waitL = 0, waitR = 0;\n    \n    for (let i = 0; i < start.length; i++) {\n        const curr = start[i];\n        const goal = target[i];\n        \n        if (curr === \'R\') {\n            if (waitL > 0) return false;\n            waitR++;\n        }\n        if (goal === \'L\') {\n            if (waitR > 0) return false;\n            waitL++;\n        }\n        if (goal === \'R\') {\n            if (waitR === 0) return false;\n            waitR--;\n        }\n        if (curr === \'L\') {\n            if (waitL === 0) return false;\n            waitL--;\n        }\n    }\n    return waitL === 0 && waitR === 0;\n};\n```\n```typescript [TS]\n// TypeScript\n\nfunction canChange(start: string, target: string): boolean {\n    if (start === target) return true;\n    let waitL = 0, waitR = 0;\n    \n    for (let i = 0; i < start.length; i++) {\n        const curr = start[i];\n        const goal = target[i];\n        \n        if (curr === \'R\') {\n            if (waitL > 0) return false;\n            waitR++;\n        }\n        if (goal === \'L\') {\n            if (waitR > 0) return false;\n            waitL++;\n        }\n        if (goal === \'R\') {\n            if (waitR === 0) return false;\n            waitR--;\n        }\n        if (curr === \'L\') {\n            if (waitL === 0) return false;\n            waitL--;\n        }\n    }\n    return waitL === 0 && waitR === 0;\n}\n```\n```rust []\nimpl Solution {\n    pub fn can_change(start: String, target: String) -> bool {\n        if start == target {\n            return true;\n        }\n        let mut wait_l = 0;\n        let mut wait_r = 0;\n        \n        let start_chars: Vec<char> = start.chars().collect();\n        let target_chars: Vec<char> = target.chars().collect();\n        \n        for i in 0..start_chars.len() {\n            let curr = start_chars[i];\n            let goal = target_chars[i];\n            \n            if curr == \'R\' {\n                if wait_l > 0 {\n                    return false;\n                }\n                wait_r += 1;\n            }\n            if goal == \'L\' {\n                if wait_r > 0 {\n                    return false;\n                }\n                wait_l += 1;\n            }\n            if goal == \'R\' {\n                if wait_r == 0 {\n                    return false;\n                }\n                wait_r -= 1;\n            }\n            if curr == \'L\' {\n                if wait_l == 0 {\n                    return false;\n                }\n                wait_l -= 1;\n            }\n        }\n        wait_l == 0 && wait_r == 0\n    }\n}\n```\n```ruby []\ndef can_change(start, target)\n    return true if start == target\n    wait_l = 0\n    wait_r = 0\n    \n    start.chars.zip(target.chars).each do |curr, goal|\n        if curr == \'R\'\n            return false if wait_l > 0\n            wait_r += 1\n        end\n        if goal == \'L\'\n            return false if wait_r > 0\n            wait_l += 1\n        end\n        if goal == \'R\'\n            return false if wait_r == 0\n            wait_r -= 1\n        end\n        if curr == \'L\'\n            return false if wait_l == 0\n            wait_l -= 1\n        end\n    end\n    wait_l == 0 && wait_r == 0\nend\n```\n\n\n- Complexity: Time O(n) & Space O(1)\n\n![image.png](https://assets.leetcode.com/users/images/6f8333d1-811f-434c-8b56-aae2950ca48e_1733364662.3132055.png)\n\n\n\n\n\n# Explanation\n\n\n---\n\n\n\n## Intuition\n\n![tlumaczenie5gru.png](https://assets.leetcode.com/users/images/033470fe-123b-4964-8e01-793137badf52_1733359329.6536765.png)\n\n- "Letters" cannot jump over another "letter"\n\n![5grutlumaczenie2.png](https://assets.leetcode.com/users/images/406e812f-e5d9-4e59-ba4e-d1ee17b36a8e_1733360756.4268253.png)\n\n\n## Approach\n\n1. Edge Case: **IF** start = target **RETURN** True \n\n```\n        if start == target:\n            return True\n```\n\n- we save on efficiency, we don\'t do the rest\n\n\n\n\n`waitL` - count waiting **L**\n`waitR` - count waiting **R**\n\n2. Core\n\n- **R** on Start:\n    - IF waiting **L**s return **False** ( **R** can\'t jump over **L**)\n        - Increment `waitR` counter\n\n- **L** in target string:\n    - IF waiting **R**s return **False** ( **L** can\'t jump over **R**)\n        - Increment `waitL` counter\n\n- **R** in target string:\n    - IF **NO** waiting **R**s return **False** (nothing to move)\n        - Decrement `waitR` counter\n\n- **L** in target string:\n    - IF **NO** waiting **L**s return **False** (nothing to move)\n        - Decrement `waitL` counter\n\n3. Check if all **L** and **R** are "matched"\n\n```\nreturn waitL == 0 and waitR == 0\n```\n\nDone. \n\n\n\n**UpVote** if you think it is helpful and will help others\n\n\n---\n\n\n\n![string.png](https://assets.leetcode.com/users/images/bf2b1f33-9164-4748-b5f4-802500c7561a_1731073630.9087918.png)\n\n\n\n\n# Most common String Interview Problems\nList based on my research (interviews, reddit, github) Probably not 100% accurate but very close to my recruitments. Leetcode Premium is probably more accurate [I don\'t have]\n\n**Easy:** [[20. Valid Parentheses]](https://leetcode.com/problems/valid-parentheses/solutions/6013115/solution) [[28. Find the Index of the First Occurrence in a String]](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string) [[13. Roman to Integer]](https://leetcode.com/problems/roman-to-integer/solutions/6021029/beats-100-explained-step-by-step-list-most-common-string-interview-problems/) [[125. Valid Palindrome]](https://leetcode.com/problems/valid-palindrome) [[680. Valid Palindrome II]](https://leetcode.com/problems/valid-palindrome-ii) [[67. Add Binary]](https://leetcode.com/problems/add-binary)\n\n\n\n\n**Medium/Hard:** [[8. String to Integer (atoi)]](https://leetcode.com/problems/string-to-integer-atoi/solutions/6013112/solution/) [[3. Longest Substring Without Repeating Characters]](https://leetcode.com/problems/longest-substring-without-repeating-characters/solutions/6013106/solutions/) [[5. Longest Palindromic Substring]](https://leetcode.com/problems/longest-palindromic-substring/solutions/6013111/solution/) [[937. Reorder Data in Log Files]](https://leetcode.com/problems/reorder-data-in-log-files) [[68. Text Justification]](https://leetcode.com/problems/text-justification) [[32. Longest Valid Parentheses]](https://leetcode.com/problems/longest-valid-parentheses) [[12. Integer to Roman]](https://leetcode.com/problems/integer-to-roman/solutions/6023402/solutions) [[22. Generate Parentheses]](https://leetcode.com/problems/generate-parentheses) [[65. Valid Number]](https://leetcode.com/problems/valid-number) [[49. Group Anagrams]](https://leetcode.com/problems/group-anagrams)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n####  [Interview Questions and Answers Repository](https://github.com/RooTinfinite/Interview-questions-and-answers)\n\n![image.png](https://assets.leetcode.com/users/images/9dc1b265-b175-4bf4-bc6c-4b188cb79220_1728176037.4402142.png)
['Swift', 'C++', 'Java', 'Go', 'TypeScript', 'Python3', 'Rust', 'Ruby', 'JavaScript', 'C#']