Skip to content

Commit 51c5673

Browse files
authored
Create move-pieces-to-obtain-a-string.py
1 parent d3a0e5f commit 51c5673

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# two pointers
5+
class Solution(object):
6+
def canChange(self, start, target):
7+
"""
8+
:type start: str
9+
:type target: str
10+
:rtype: bool
11+
"""
12+
i = j = 0
13+
while True:
14+
while i < len(start) and start[i] == '_':
15+
i += 1
16+
while j < len(target) and target[j] == '_':
17+
j += 1
18+
if i == len(start) and j == len(target):
19+
break
20+
if i == len(start) or j == len(target) or start[i] != target[j] or \
21+
(start[i] == 'L' and i < j) or (start[i] == 'R' and i > j):
22+
return False
23+
i += 1
24+
j += 1
25+
return True

0 commit comments

Comments
 (0)