1138. Alphabet Board Path
On This Page
class Solution:
def alphabetBoardPath(self, target: str) -> str:
i = j = 0
res = []
for c in target:
v = ord(c) - ord("a")
x = v // 5
y = v % 5
while j > y:
j -= 1
res.append("L")
while i > x:
i -= 1
res.append("U")
while j < y:
j += 1
res.append("R")
while i < x:
i += 1
res.append("D")
res.append("!")
return "".join(res)