File tree Expand file tree Collapse file tree 1 file changed +12
-13
lines changed Expand file tree Collapse file tree 1 file changed +12
-13
lines changed Original file line number Diff line number Diff line change 1
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 ++);
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 ;
7
14
}
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
- }
16
15
}
You can’t perform that action at this time.
0 commit comments