December 05, 2024
์ด ๋ฌธ์ ๋ 'L', 'R', '_'๋ก ์ด๋ฃจ์ด์ง ๋ ๋ฌธ์์ด์ด ์ฃผ์ด์ง ๋, ๋ค์ ์ฐ์ฐ์ ํตํด ๋ ๋ฌธ์์ด์ด ๋๊ฐ์์ง ์ ์๋์ง ํ์ธํ๋ ๋ฌธ์ ์ด๋ค.
class Solution:
def canChange(self, start: str, target: str) -> bool:
# if order is different return False
if start.replace('_', '') != target.replace('_', ''):
return False
n = len(start)
i = j = 0
while i < n and j < n:
# skip underscores at start
while i < n and start[i] == '_':
i += 1
while j < n and target[j] == '_':
j += 1
# if exhausted, they should both be equal
if i == n or j == n:
return i == j
# check if pieces match
if (start[i] != target[j]) or \
(start[i] == 'L' and i < j) or \
(start[i] == 'R' and i > j):
return False
i += 1
j += 1
return True
Constraints:
n == start.length == target.length
1 <= n <= 105
start and target consist of the characters 'L', 'R', and '_'.