This commit is contained in:
parent
635a34eb91
commit
511ce16e0f
1 changed files with 27 additions and 0 deletions
27
medium/move_pieces_to_obtain_a_string.py
Normal file
27
medium/move_pieces_to_obtain_a_string.py
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
# https://leetcode.com/problems/move-pieces-to-obtain-a-string
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def canChange(self, start: str, target: str) -> bool:
|
||||||
|
length = len(start)
|
||||||
|
start_char_list = []
|
||||||
|
target_char_list = []
|
||||||
|
for i in range(length):
|
||||||
|
if start[i] != '_':
|
||||||
|
start_char_list.append([start[i], i])
|
||||||
|
if target[i] != '_':
|
||||||
|
target_char_list.append([target[i], i])
|
||||||
|
|
||||||
|
if len(start_char_list) != len(target_char_list):
|
||||||
|
return False
|
||||||
|
|
||||||
|
while len(start_char_list) > 0:
|
||||||
|
start_char, start_index = start_char_list.pop()
|
||||||
|
target_char, target_index = target_char_list.pop()
|
||||||
|
not_okay = start_char != target_char or \
|
||||||
|
(start_char == 'L' and start_index < target_index) or \
|
||||||
|
(start_char == 'R' and start_index > target_index)
|
||||||
|
if not_okay:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue