There is a special typewriter with lowercase English letters 'a' to 'z' arranged in a circle with a pointer. A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character 'a'.
Each second, you may perform one of the following operations:
- Move the pointer one character counterclockwise or clockwise.
- Type the character the pointer is currently on.
Given a string word, return the minimum number of seconds to type out the characters in word.
Example 1:
Input: word = "abc"
Output: 5
Explanation:
The characters are printed as follows:
- Type the character 'a' in 1 second since the pointer is initially on 'a'.
- Move the pointer clockwise to 'b' in 1 second.
- Type the character 'b' in 1 second.
- Move the pointer clockwise to 'c' in 1 second.
- Type the character 'c' in 1 second.
Example 2:
Input: word = "bza"
Output: 7
Explanation:
The characters are printed as follows:
- Move the pointer clockwise to 'b' in 1 second.
- Type the character 'b' in 1 second.
- Move the pointer counterclockwise to 'z' in 2 seconds.
- Type the character 'z' in 1 second.
- Move the pointer clockwise to 'a' in 1 second.
- Type the character 'a' in 1 second.
Example 3:
Input: word = "zjpc"
Output: 34
Explanation:
The characters are printed as follows:
- Move the pointer counterclockwise to 'z' in 1 second.
- Type the character 'z' in 1 second.
- Move the pointer clockwise to 'j' in 10 seconds.
- Type the character 'j' in 1 second.
- Move the pointer clockwise to 'p' in 6 seconds.
- Type the character 'p' in 1 second.
- Move the pointer counterclockwise to 'c' in 13 seconds.
- Type the character 'c' in 1 second.
Constraints:
- 1 <= word.length <= 100
- word consists of lowercase English letters.
class Solution:
def minTimeToType(self, word: str) -> int:
place=1
ans=0
fst = "a"
for i in range(len(word)):
snd = word[i]
a=( abs(idx(fst)-idx(snd)) )
b=(26 - ( max(idx(fst),idx(snd)) - min(idx(fst),idx(snd))))
ans+=min(a,b)+1
fst=word[i]
return ans
def idx(char):
return ord(char)-96
'알고리즘 문제 > Leetcode' 카테고리의 다른 글
2144. Minimum Cost of Buying Candies With Discount (0) | 2022.12.04 |
---|---|
2027. Minimum Moves to Convert String (1) | 2022.12.03 |
1903. Largest Odd Number in String (0) | 2022.12.02 |
1827. Minimum Operations to Make the Array Increasing (0) | 2022.12.02 |
1736. Latest Time by Replacing Hidden Digits (0) | 2022.12.02 |