Skip to content

Commit 2c72231

Browse files
solves minimum time to type word using special typewriter
1 parent 79a2adb commit 2c72231

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@
473473
| 1961 | [Check If String Is a Prefix of Array](https://leetcode.com/problems/check-if-string-is-a-prefix-of-array) | [![Java](assets/java.png)](src/CheckIfStringIsAPrefixOfArray.java) | |
474474
| 1967 | [Number of Strings That Appear as Substrings in Word](https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word) | [![Java](assets/java.png)](src/NumberOfStringsThatAppearAsSubstringsInWord.java) | |
475475
| 1971 | [Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph) | [![Java](assets/java.png)](src/FindIfPathExistsInGraph.java) | |
476-
| 1974 | [Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter) | | |
476+
| 1974 | [Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter) | [![Java](assets/java.png)](src/MinimumTimeToTypeWordUsingSpecialTypewriter.java) | |
477477
| 1979 | [Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array) | | |
478478
| 1984 | [Minimum Difference Between Highest and Lowest of K Scores](https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores) | | |
479479
| 1991 | [Find the Middle Index in Array](https://leetcode.com/problems/find-the-middle-index-in-array) | | |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter
2+
// T: O(|word|)
3+
// S: O(1)
4+
5+
public class MinimumTimeToTypeWordUsingSpecialTypewriter {
6+
public int minTimeToType(String word) {
7+
return movingTime(word) + word.length();
8+
}
9+
10+
private int movingTime(String s) {
11+
int movingTime = 0, distance;
12+
for (int i = 1 ; i < s.length() ; i++) {
13+
distance = Math.abs(s.charAt(i) - s.charAt(i - 1));
14+
movingTime += Math.min(distance, 26 - distance);
15+
}
16+
distance = Math.abs(s.charAt(0) - 'a');
17+
return movingTime + Math.min(distance, 26 - distance);
18+
}
19+
}

0 commit comments

Comments
 (0)