Skip to content

Commit 290f75f

Browse files
authored
Update Single-Row Keyboard.java
1 parent 17c91bf commit 290f75f

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

Easy/Single-Row Keyboard.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
class Solution {
2-
public int calculateTime(String keyboard, String word) {
3-
Map<Character, Integer> map = new HashMap<>();
4-
int pos = 0;
5-
for (char c : keyboard.toCharArray()) {
6-
map.put(c, pos++);
2+
public int calculateTime(String keyboard, String word) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
for (int i = 0; i < 26; i++) {
5+
map.put(keyboard.charAt(i), i);
6+
}
7+
int position = 0;
8+
int time = 0;
9+
for (char c : word.toCharArray()) {
10+
time += Math.abs(position - map.get(c));
11+
position = map.get(c);
12+
}
13+
return time;
714
}
8-
int total = 0;
9-
int currPos = 0;
10-
for (char c : word.toCharArray()) {
11-
total += Math.abs(currPos - map.get(c));
12-
currPos = map.get(c);
13-
}
14-
return total;
15-
}
1615
}

0 commit comments

Comments
 (0)