-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabetBoardPath.py
More file actions
34 lines (32 loc) · 1000 Bytes
/
alphabetBoardPath.py
File metadata and controls
34 lines (32 loc) · 1000 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/alphabet-board-path/
# Author: Miao Zhang
# Date: 2021-04-12
class Solution:
def alphabetBoardPath(self, target: str) -> str:
paths = [['' for _ in range(26)] for _ in range(26)]
for s in range(26):
for t in range(26):
dx = t // 5 - s // 5
dy = t % 5 - s % 5
path = ''
while dx < 0:
path += 'U'
dx += 1
while dy < 0:
path += 'L'
dy += 1
while dx > 0:
path += 'D'
dx -= 1
while dy > 0:
path += 'R'
dy -= 1
paths[s][t] = path
res = ''
s = 'a'
for c in target:
res += paths[ord(s) - ord('a')][ord(c) - ord('a')] + '!'
s = c
return res