You have a special circular keyboard with all 26 lowercase English letters arranged in a circle. The letters are ordered from 'a' to 'z' in clockwise order, and after 'z' comes 'a' again (forming a circle).
A pointer initially points to the letter 'a'. To type a word on this keyboard, you need to:
Each operation takes exactly 1 second:
Given a string word consisting of lowercase English letters, calculate the minimum number of seconds needed to type the entire word.
word consists of lowercase English letters onlyExample 1:
` Input: word = "abc" Output: 5 Explanation:
Example 2:
` Input: word = "bza" Output: 7 Explanation:
Example 3:
` Input: word = "z" Output: 2 Explanation:
Hint 1: Distance Calculation For a circular arrangement of 26 letters, think about how to calculate the shortest distance between any two letters. If you're at position
iand need to reach positionj, you can go clockwise or counterclockwise. The shortest distance is the minimum of these two paths.
Hint 2: Formula for Circular Distance Given two positions on a circle with 26 positions, the clockwise distance is
abs(target - current), and the counterclockwise distance is26 - abs(target - current). The minimum of these two values gives you the shortest path.
Hint 3: Track Current Position You don't need to simulate the actual movement. Just keep track of your current position (starting at 'a' or position 0), calculate the minimum distance to the next character, add 1 for typing, and update your current position.
Full Solution ` def minTimeToType(word: str) -> int: # Start at position 'a' (position 0) current_pos = 0 total_time = 0
for char in word: # Convert character to position (0-25) target_pos = ord(char) - ord('a') # Calculate the direct distance (could be clockwise or counterclockwise) direct_distance = abs(target_pos - current_pos) # Calculate the wrap-around distance (going the other way around the circle) wrap_distance = 26 - direct_distance # Take the minimum of the two possible paths min_distance = min(direct_distance, wrap_distance) # Add movement time + typing time (1 second) total_time += min_distance + 1 # Update current position current_pos = target_pos return total_time`
Explanation:
The solution works by:
Tracking Position: We maintain
current_posstarting at 0 (letter 'a')Converting Characters: For each character in the word, we convert it to a numerical position (0-25) using
ord(char) - ord('a')Finding Shortest Path: On a circular keyboard with 26 positions:
- The direct distance is
abs(target_pos - current_pos)- The wrap-around distance (going the opposite direction) is
26 - direct_distance- We take the minimum of these two values
Accumulating Time: For each character, we add:
- The minimum movement distance
- Plus 1 second for typing
Updating Position: After processing each character, we update our current position
Time Complexity: O(n) where n is the length of the word. We iterate through each character once.
Space Complexity: O(1) as we only use a constant amount of extra space for variables.