We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 17c91bf commit 290f75fCopy full SHA for 290f75f
Easy/Single-Row Keyboard.java
@@ -1,16 +1,15 @@
1
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++);
+ public int calculateTime(String keyboard, String word) {
+ Map<Character, Integer> map = new HashMap<>();
+ for (int i = 0; i < 26; i++) {
+ map.put(keyboard.charAt(i), i);
+ }
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;
14
}
- int total = 0;
- int currPos = 0;
- for (char c : word.toCharArray()) {
- total += Math.abs(currPos - map.get(c));
- currPos = map.get(c);
- }
- return total;
15
16
0 commit comments