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 <= 100word consists of lowercase English letters.Problem Overview: You start with a pointer on character 'a' of a circular typewriter containing the letters a to z. For each character in the target word, rotate the pointer either clockwise or counterclockwise to reach the letter, then press the key to type it. The goal is to compute the minimum total time required.
Approach 1: Simple Circular Distance Calculation (Greedy) (Time: O(n), Space: O(1))
The alphabet forms a circle of 26 characters. Moving from one letter to another has two possible paths: clockwise and counterclockwise. For each character in the word, compute the distance from the current pointer position using abs(curr - target). The opposite direction distance is 26 - diff. Choose the smaller value and add 1 second for pressing the key. Update the pointer to the typed character and continue scanning the string.
This works because each step is independent. The optimal move between two letters is always the shorter circular distance. No dynamic programming or backtracking is required. The algorithm simply iterates once through the word, making it linear time. This problem mainly tests understanding of circular distance calculations in string problems and applying a straightforward greedy decision at every step.
Approach 2: Prefix Sum Optimization (Time: O(n), Space: O(n))
If multiple distance queries were required, you could precompute cumulative rotation costs using a prefix structure. Convert each character into an index from 0–25 and precompute distances between adjacent characters in the word. A prefix sum array stores cumulative typing cost so that partial ranges can be evaluated quickly. Each transition still uses the same circular distance formula, but prefix sums make it easy to reuse computed movement costs.
For this specific problem the prefix sum approach does not improve asymptotic complexity. The word is processed once and each step is constant work, so the greedy scan is already optimal. Prefix sums mainly demonstrate how repeated movement computations could be aggregated when the problem expands to multiple queries or substring evaluations. It connects with common preprocessing patterns used in string traversal problems.
Recommended for interviews: The greedy circular distance solution is what interviewers expect. It shows you recognized the alphabet is a ring and minimized rotation using min(diff, 26 - diff). Mentioning the prefix-sum idea demonstrates awareness of preprocessing techniques, but the O(n) greedy scan is the clean and optimal implementation.
This approach simplifies the problem by calculating the distance between each consecutive character in the word, taking advantage of the circular arrangement. The cost to move the pointer from one character to another is the minimum of the clockwise and counterclockwise distances. The algorithm iterates over the characters of the word, maintaining a total cost variable to track the number of seconds taken.
This C implementation iterates through the word, calculating the minimum distance between the current pointer position and the target character, accounting for both clockwise and counterclockwise directions. Each movement calculation adds the longer of clockwise or counterclockwise paths to the time, plus one second to type the character.
Time Complexity: O(n) where n is the length of the word.
Space Complexity: O(1).
This approach employs a prefix sum-like optimization, where the traversal through the word strives to minimize the rotational efforts by pre-calculating movement directions and remembering previous movement choices. It considers the word as a sequence of weights, emphasizing preemptive minimization for longer strings where potential repetitive patterns might occur.
In this C solution, the rotation is done by analyzing and comparing costs, summarizing the costs from accumulated calculations. This version seeks to highlight the balance and ensures every previous choice-governed movement cost is recorded succinctly.
Time Complexity: O(n) where n is the length of the word.
Space Complexity: O(1).
We initialize the answer variable ans to the length of the string, indicating that we need at least ans seconds to type the string.
Next, we traverse the string. For each character, we calculate the minimum distance between the current character and the previous character, and add this distance to the answer. Then we update the current character to the previous character and continue traversing.
The time complexity is O(n), where n is the length of the string. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Simple Circular Distance Calculation | Time Complexity: O(n) where n is the length of the word. |
| Prefix Sum Optimization | Time Complexity: O(n) where n is the length of the word. |
| Greedy | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Circular Distance Calculation (Greedy) | O(n) | O(1) | Best for the standard problem. Single pass through the word with constant memory. |
| Prefix Sum Optimization | O(n) | O(n) | Useful when extending the problem to multiple queries or repeated substring cost calculations. |
5834. Minimum Time to Type Word Using Special Typewriter | Leetcode Biweekly Contest 59 | Solution • Abhinav Awasthi • 924 views views
Watch 9 more video solutions →Practice Minimum Time to Type Word Using Special Typewriter with our built-in code editor and test cases.
Practice on FleetCode