Leetcode Problem 2337. Move Pieces to Obtain a String

2337. Move Pieces to Obtain a String

Leetcode Solutions

Two Pointer Approach

  1. Initialize two pointers, i and j, to 0.
  2. Iterate over both strings until at least one pointer reaches the end of the string.
  3. Increment each pointer until it points to a non-'_' character or reaches the end of the string.
  4. If both pointers are at the end, return true as all checks have passed.
  5. If one pointer is at the end but the other is not, return false as the strings have different numbers of 'L' and 'R'.
  6. If the characters at the current pointers are different, return false.
  7. If the character is 'L' and the pointer in start is less than the pointer in target, return false.
  8. If the character is 'R' and the pointer in start is greater than the pointer in target, return false.
  9. Increment both pointers and continue the iteration.
  10. If the end of both strings is reached without returning false, return true.
UML Thumbnail

Queue-Based Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...