-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabetBoardPath.cpp
More file actions
42 lines (40 loc) · 1.12 KB
/
alphabetBoardPath.cpp
File metadata and controls
42 lines (40 loc) · 1.12 KB
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
36
37
38
39
40
41
42
// Source: https://leetcode.com/problems/alphabet-board-path/
// Author: Miao Zhang
// Date: 2021-04-12
class Solution {
public:
string alphabetBoardPath(string target) {
vector<vector<string>> paths(26, vector<string>(26));
for (int s = 0; s < 26; s++) {
for (int t = 0; t < 26; t++) {
int dx = t / 5 - s / 5;
int dy = t % 5 - s % 5;
string path;
while (dx < 0) {
path.push_back('U');
dx++;
}
while (dy < 0) {
path.push_back('L');
dy++;
}
while (dx > 0) {
path.push_back('D');
dx--;
}
while (dy > 0) {
path.push_back('R');
dy--;
}
paths[s][t] = path;
}
}
char s = 'a';
string res;
for (char c: target) {
res += paths[s - 'a'][c - 'a'] + "!";
s = c;
}
return res;
}
};