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:
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.The problem involves finding the minimum time to type a given word using a special typewriter where characters are arranged in a circle. The solution leverages a greedy approach, focusing on minimizing the movement of the pointer at each step.
Core Idea:
The optimal strategy is to always move the pointer in the shortest direction (clockwise or counter-clockwise) to reach the next character. The time taken to type a character is the sum of the time to move the pointer and the time to type the character itself (which is always 1 second).
Algorithm:
Initialization:
ans
: Variable to store the total time. Initialized to 0.prev
: Variable to keep track of the index of the previously typed character (initially 'a', index 0).Iteration:
c
) in the input word
.curr
) by subtracting the ASCII value of 'a' from c
.t
) between the previous character's index (prev
) and the current character's index (curr
). This involves finding the minimum of the absolute difference and its complement (26 - absolute difference) to account for the circular nature of the arrangement.t
) plus 1 (for typing the character) to the total time (ans
).prev
with curr
for the next iteration.Return:
ans
).Time Complexity: O(n), where n is the length of the word. We iterate through the word once.
Space Complexity: O(1). We use only a few constant extra variables.
class Solution:
def minTimeToType(self, word: str) -> int:
ans = prev = 0
for c in word:
curr = ord(c) - ord('a') #Efficiently gets the index of the character
t = abs(prev - curr) #Distance between previous and current
t = min(t, 26 - t) #Shortest distance considering circularity
ans += t + 1 #Time to move + time to type
prev = curr #Update previous character index
return ans
The Python code directly implements the algorithm described above. The ord()
function is used for efficient index calculation. The min()
function elegantly handles the shortest-distance calculation.
The Java, C++, and Go code follow the same logic as the Python code, with minor syntactic differences to reflect the specific features of each language. All of them maintain the same time and space complexity. The key aspects—the iteration, distance calculation (including the circularity handling), and time accumulation—remain consistent across all implementations.