2414. Move Pieces to Obtain a String
My accepted Python solution to LeetCode problem 2414, Move Pieces to Obtain a String, running in 111ms.
- Difficulty: Medium
- Python
- Runtime 111ms
- Memory 17.9MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 111ms, memory 17.9MB, accepted 2025-12-29.
class Solution:
def canChange(self, start: str, target: str) -> bool:
# Remove underscores and compare - the relative order of L and R must be same
if start.replace('_', '') != target.replace('_', ''):
return False
n = len(start)
j = 0
for i in range(n):
if start[i] == '_':
continue
# Find the matching character in target
while j < n and target[j] == '_':
j += 1
# L can only move left, so start index should be >= target index
if start[i] == 'L' and i < j:
return False
# R can only move right, so start index should be <= target index
if start[i] == 'R' and i > j:
return False
j += 1
return True