-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalphabet-board-path.py
34 lines (34 loc) · 986 Bytes
/
alphabet-board-path.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
class Solution:
def alphabetBoardPath(self, target: str) -> str:
res = []
prev = (0, 0)
for i, c in enumerate(target):
pos = ord(c) - ord('a')
curr = (pos // 5, pos % 5)
px, py = prev
cx, cy = curr
def xmove():
nonlocal px, cx
while px < cx:
res.append('D')
px += 1
while px > cx:
res.append('U')
px -= 1
def ymove():
nonlocal py, cy
while py < cy:
res.append('R')
py += 1
while py > cy:
res.append('L')
py -= 1
if c == 'z':
ymove()
xmove()
else:
xmove()
ymove()
res.append('!')
prev = curr
return "".join(res)