- Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalphabet-board-path.cpp
44 lines (36 loc) · 919 Bytes
/
alphabet-board-path.cpp
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
43
44
// Runtime: 4 ms
// Memory Usage: 8.6 MB
classSolution {
public:
string alphabetBoardPath(string target) {
vector<string> board = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
map<char, pair<int, int> > mp;
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
mp[board[i][j]] = {i, j};
}
}
intx(0), y(0);
string res = "";
for (char a : target) {
int xi = mp[a].first;
int yi = mp[a].second;
if (y > yi) {
for (int i = 0; i < abs(y - yi); i++) res += "L";
}
if (x > xi) {
for (int i = 0; i < abs(x - xi); i++) res += "U";
}
if (y < yi) {
for (int i = 0; i < abs(y - yi); i++) res += "R";
}
if (x < xi) {
for (int i = 0; i < abs(x - xi); i++) res += "D";
}
x = xi;
y = yi;
res += "!";
}
return res;
}
};