forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphabet Board Path.py
35 lines (34 loc) · 1.08 KB
/
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
35
# Runtime: 68 ms (Top 16.25%) | Memory: 14 MB (Top 13.50%)
class Solution:
def alphabetBoardPath(self, target: str) -> str:
def shortestPath(r,c,tr,tc):
path = ""
pr = r
while r > tr:
path += 'U'
r -= 1
while r < tr:
path += 'D'
r += 1
if tr == 5 and pr != tr: path = path[:len(path) - 1]
while c > tc:
path += 'L'
c -= 1
while c < tc:
path += 'R'
c += 1
if tr == 5 and pr != tr: path = path + 'D'
return path
table = ['abcde','fghij','klmno','pqrst','uvwxy','z']
r,c = 0,0
ans = ""
for character in target:
t_row = None
for i,word in enumerate(table):
if character in word:
t_row = i
break
t_col = table[i].index(character)
ans += shortestPath(r,c,t_row,t_col) + '!'
r,c = t_row,t_col
return ans