Skip to content

Commit b9d321d

Browse files
add solution for 1165
1 parent 19adb54 commit b9d321d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/main/java/com/fishercoder/solutions/_1165.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929
public class _1165 {
3030
public static class Solution1 {
3131
public int calculateTime(String keyboard, String word) {
32-
return -1;
32+
int time = 0;
33+
int fromIndex = 0;
34+
int fingerMoves;
35+
for (char c : word.toCharArray()) {
36+
fingerMoves = Math.abs(fromIndex - keyboard.indexOf(c));
37+
fromIndex = keyboard.indexOf(c);
38+
time += fingerMoves;
39+
}
40+
return time;
3341
}
3442
}
3543
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1165;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1165Test {
10+
private static _1165.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1165.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(4, solution1.calculateTime("abcdefghijklmnopqrstuvwxyz", "cba"));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(73, solution1.calculateTime("pqrstuvwxyzabcdefghijklmno", "leetcode"));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)