-
Notifications
You must be signed in to change notification settings - Fork 17
/
move-pieces-to-obtain-a-string.py
53 lines (42 loc) · 1.55 KB
/
move-pieces-to-obtain-a-string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import Counter
class Solution:
def canChange(self, start: str, target: str) -> bool:
if Counter(start) != Counter(target):
return False
spaces = 0
start_pos = 0
start_arr = list(start)
target_arr = list(target)
left = "L"
for _ in range(2):
for target_pos in range(len(target)):
if target_arr[target_pos] == "_":
continue
while start_pos < len(start) and start_arr[start_pos] == "_":
start_pos += 1
spaces += 1
if (
start_pos == len(start_arr)
or start_arr[start_pos] != target_arr[target_pos]
):
return False
if start_arr[start_pos] == left:
if start_pos < target_pos:
return False
else:
if spaces < start_pos - target_pos:
return False
else:
spaces = start_pos - target_pos
if start_pos != target_pos:
start_arr[target_pos] = target_arr[target_pos]
start_arr[start_pos] = "_"
else:
spaces = 0
start_pos += 1
start_arr.reverse()
target_arr.reverse()
left = "R"
spaces = 0
start_pos = 0
return True