2337. Move Pieces to Obtain a String - 100%
Approach\nThe approach to the solution is described in the comments in the code, perhaps it will be clearer this way.\n\n# Complexity\n- Time complexity:\nO(n)\
# Approach\nThe approach to the solution is described in the comments in the code, perhaps it will be clearer this way.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```typescript []\nfunction canChange(start: string, target: string): boolean {\n    // initialize counters for encountered characters\n    // if we encounter a character in target - increment\n    // if we encounter a character in start - decrement\n    // \'_\' characters are not important\n    let countR = 0;\n    let countL = 0;\n\n    // if start and target are equal - immediately return true\n    if (start === target) return true;\n\n    for (let targetIndex = 0; targetIndex < target.length; targetIndex++) {\n        // at each step, get the characters\n        const letterStart = start[targetIndex];\n        const letterTarget = target[targetIndex];\n\n        // check the obtained values. The order of checks is important!\n        // if we encounter \'R\' in target\n        // 1. if we expect \'L\' in target (countL > 0) - return false, because \'R\' has blocked its path\n        // 2. \'R\' can only move to the right, so if we already expect \'R\' in target (countR > 0) - \n        // return false, because \'R\' from letterStart cannot move to the left\n        if (letterStart === \'R\') {\n            if (countL > 0) return false;\n            if (countR > 0) return false;\n            countR--;\n        }\n\n        // if we encounter \'R\' in target while already expecting \'R\' (countR > 0) return false\n        // because \'R\' characters in start should appear earlier or simultaneously with target.\n        // And in such a case, countR should have been <= 0\n        if (letterTarget === \'R\') {\n            if (countR > 0) return false;\n            countR++;\n        }\n\n        // if we encounter \'L\' in target and \'R\' is expected after start (countR < 0),\n        // the letters overlap, return false\n        if (letterTarget === \'L\') {\n            if (countR < 0) return false;\n            countL++;\n        }\n\n        // if we encounter \'L\' in start and \'L\' is not needed (countL === 0), return false\n        // \'L\' can move to the left, so \'L\' in target should have been encountered simultaneously\n        // or earlier than in start, meaning countL would be greater than 0\n        if (letterStart === \'L\') {\n            if (countL === 0) return false;\n            countL--;\n        }\n\n    }\n\n    // if any counter is not equal to 0, then the number of characters in start and target do not match\n    if (countL !== 0 || countR !== 0) return false;\n\n    return true;\n};\n```
['Two Pointers', 'String', 'TypeScript']