1138. Alphabet Board Path

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1138

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)