Skip to main content

Minimum Rotations to Dial a Number I - Solution & Explanation

Easy8 min read
Practice this problem

Problem Statement

You are given a string s of length 10 consisting of digits.

The dial contains the digits 0 through 9 in order and is circular, so 0 and 9 are adjacent. The pointer initially points to 0.

To dial each digit of s in order, rotate the pointer until it points to that digit. Each rotation moves the pointer to an adjacent digit, and you may rotate in either direction. Dialing a digit that the pointer already points to requires no rotations.

Return the minimum total number of rotations needed to dial every digit of s.

 

Example 1:

Input: s = "0192837465"

Output: 25

Explanation:

Step From To Rotations
1 0 0 0
2 0 1 1
3 1 9 2
4 9 2 3
5 2 8 4
6 8 3 5
7 3 7 4
8 7 4 3
9 4 6 2
10 6 5 1

The total is 0 + 1 + 2 + 3 + 4 + 5 + 4 + 3 + 2 + 1 = 25, which is the minimum total number of rotations.

Example 2:

Input: s = "1200210200"

Output: 12

Explanation:

Step From To Rotations
1 0 1 1
2 1 2 1
3 2 0 2
4 0 0 0
5 0 2 2
6 2 1 1
7 1 0 1
8 0 2 2
9 2 0 2
10 0 0 0

The total is 1 + 1 + 2 + 0 + 2 + 1 + 1 + 2 + 2 + 0 = 12, which is the minimum total number of rotations.

 

Constraints:

  • s.length == 10
  • s consists only of digits '0' to '9'

Approach Overview

Problem Overview: You have a circular dial with digits 0 through 9. The pointer starts at digit 0, and you must dial each digit of the target number in order. Each rotation moves the pointer one step clockwise or counterclockwise, and you want the minimum total rotations.

Approach 1: Step-by-Step Simulation (O(n * 10) time, O(1) space)

Track the current pointer position and move it one step at a time toward each target digit. For each digit, keep rotating in one direction until you reach the target. This works, but it does unnecessary work because the dial has only 10 positions and each digit can require up to 5 steps in one direction. Use it to confirm your understanding of the circular movement before optimizing.

Approach 2: Circular Distance Per Digit (O(n) time, O(1) space)

For each consecutive pair of positions, compute the absolute difference d = |current - target|. On a circle of 10 digits, you can reach the target either by going forward d steps or backward 10 - d steps. Take min(d, 10 - d) and add it to the total. Update the current position to the target and move on. Each digit is handled with a constant amount of arithmetic, so there is no inner loop. This is the approach you should write in an interview. Use the array traversal and simple arithmetic here, with no extra data structures needed.

Approach 3: Precomputed Distance Table (O(n) time, O(1) space)

You can build a 10 x 10 table of minimum circular distances once and look up each transition in constant time. This is functionally identical to Approach 2 and only helps if the distance logic is more complex than a single formula. For this problem, the direct formula is shorter and less error-prone.

Recommended for interviews: Interviewers expect the simulation idea first, because it shows you understand how the circular dial works. The circular distance formula min(d, 10 - d) is what they want to see as the final answer. Explaining why both directions must be considered is what separates a strong answer from a weak one. Mention the O(n) time and O(1) space bounds explicitly when you present the solution. Related reading: simulation and math.

Solution

Thinking

Each step only depends on the current pointer position and the next digit to dial, so we can process the string greedily from left to right.

If we move from digit a to digit b, the clockwise and counterclockwise distances add up to 10. Let d=|a-b|, then the minimum cost of this move is min(d, 10-d).

Starting from digit 0, we sum this minimum cost for every transition.

Let pre be the previous digit under the pointer and cur be the next target digit. Because the dial is circular, the shortest distance between them is

$ min\bigl(|cur-pre|, 10-|cur-pre|\bigr).

We iterate through the string, add this value to the answer, and update pre to cur.

The time complexity is O(n), and the space complexity is O(1). Here, n is the length of s$.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Step-by-Step SimulationO(n * 10)O(1)Useful for understanding the problem, not for submission
Circular Distance Per DigitO(n)O(1)General case and interview answer
Precomputed Distance TableO(n)O(1) extra (100 entries)When transition costs are irregular

Video Solutions (8)

Leetcode 4071 | Minimum Rotations to Dial a Number II | Leetcode weekly contest 522

CodeWithMeGuys14:18590 views
Video 1 of 8

Frequently Asked Questions

Is Minimum Rotations to Dial a Number I easy or hard?
It is rated Easy with a 75.8% acceptance rate. The main challenge is recognizing that the dial wraps around, so you must consider both directions. Once you see the min(d, 10 - d) pattern, the implementation takes only a few lines.
Minimum Rotations to Dial a Number I Python solution?
In Python, convert each character to an integer, keep a variable for the current position starting at 0, and for each digit compute d = abs(target - current), then add min(d, 10 - d) to the total. Return the total after the loop. The code is short and runs in O(n) time with O(1) extra space.
How to solve Minimum Rotations to Dial a Number I in O(n)?
Start the pointer at digit 0 and iterate through the target digits in order. For each digit, compute the absolute difference from the current position, take the smaller of that value and 10 minus that value, add it to the total, and set the current position to the target digit.
What is the best approach for Minimum Rotations to Dial a Number I?
The best approach is to compute the circular distance between each consecutive pair of digits using min(d, 10 - d), where d is the absolute difference. Sum these values while walking through the number once. This runs in O(n) time and O(1) space.
What data structure is used in Minimum Rotations to Dial a Number I?
No special data structure is required. The solution uses a few integer variables to track the current digit position and the running total. Simple iteration over the input string or digit array is enough.
What is the time complexity of Minimum Rotations to Dial a Number I?
The optimal solution runs in O(n) time, where n is the number of digits to dial, because each digit requires only constant-time arithmetic. The space complexity is O(1) since you only store the current position and a running total.
Is Minimum Rotations to Dial a Number I asked at Google, Amazon, or Meta?
This problem is tagged as General on FleetCode and does not have a specific company tag in our data. Check recent interview reports on company-specific sites before assuming it appears at a particular firm. The underlying skill, modular circular distance, is useful across many interview problems.

Ready to solve this problem?

Practice Minimum Rotations to Dial a Number I with our built-in code editor and test cases.

Practice on FleetCode