Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b991a08

Browse files
authoredJul 28, 2019
Create alphabet-board-path.cpp
1 parent 30e1752 commit b991a08

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

‎C++/alphabet-board-path.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string alphabetBoardPath(string target) {
7+
string result;
8+
int x = 0, y = 0;
9+
for (const auto& c : target) {
10+
int x1 = (c - 'a') % 5, y1 = (c - 'a') / 5;
11+
result += string(max(y - y1, 0), 'U');
12+
result += string(max(x - x1, 0), 'L');
13+
result += string(max(x1 - x, 0), 'R');
14+
result += string(max(y1 - y, 0), 'D');
15+
result += "!";
16+
x = x1, y = y1;
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)
Please sign in to comment.