#2337
Move Pieces to Obtain a String
specialist · 765 · lc medium +31 · verified · 56.7% accepted · 1,460 likes · top 51%
Description
You are given two strings start and target of equal length n, both composed of 'L', 'R', and '_':
- An 'L' piece may slide left into an immediately adjacent blank.
- An 'R' piece may slide right into an immediately adjacent blank.
- '_' is a blank space that any piece may enter.
Return true if start can be transformed into target through any number of valid moves, otherwise return false.
Example 1:
Input: start = "_L__R__R_", target = "L______RR"
Output: true
Explanation: We can obtain the string target from start by doing the following moves:
- Move the first piece one step to the left, start becomes equal to "L___R__R_".
- Move the last piece one step to the right, start becomes equal to "L___R___R".
- Move the second piece three steps to the right, start becomes equal to "L______RR".
Since it is possible to get the string target from start, we return true.
Example 2:
Input: start = "R_L_", target = "__LR"
Output: false
Explanation: The 'R' piece in the string start can move one step to the right to obtain "_RL_".
After that, no pieces can move anymore, so it is impossible to obtain the string target from start.
Example 3:
Input: start = "_R", target = "R_"
Output: false
Explanation: The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.
Code
1
2
3