Rotated Digits - Complete Solution Guide
Rotated Digits is LeetCode problem 788, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
An integer x is a good if after rotating each digit individually by 180 degrees, we get a valid number that is different from x . Each digit must be rotated - we cannot choose to leave it alone. A number is valid if each digit remains a digit after rotation. For example: 0 , 1 , and 8 rotate to themselves, 2 and 5 rotate to each other (in this case they are rotated in a different direction, in other words, 2 or 5 gets mirrored), 6 and 9 rotate to each other, and the rest of the numbers do not ro
Detailed Explanation
The problem asks us to find the number of 'good' integers within a given range [1, n]. A 'good' integer is defined as one that, after rotating each digit individually by 180 degrees, becomes a valid number that is *different* from the original number. The rotations are as follows: 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other; and 3, 4, and 7 do not rotate to any digit and become invalid. The input is a single integer 'n', and the output is the count of 'good' numbers between 1 and 'n' (inclusive). The constraint is that 1 <= n <= 10^4.
Solution Approach
The provided code iterates through each number from 1 to 'n'. For each number, it converts it into a string to easily check for the presence of specific digits. It first checks if the string contains any invalid digits ('3', '4', or '7'). If it does, the number is skipped. If the number is valid (no invalid digits), it then checks if the string contains any of the 'good' digits ('2', '5', '6', or '9'). If it does, the count of 'good' numbers is incremented. Finally, the total count of 'good' numbers is returned.
Step-by-Step Algorithm
- Step 1: Initialize a counter 'count' to 0. This counter will store the number of 'good' integers.
- Step 2: Iterate through each number 'i' from 1 to 'n' (inclusive).
- Step 3: Convert the number 'i' to a string 's'.
- Step 4: Check if the string 's' contains any of the invalid digits '3', '4', or '7'. If it does, the number is not valid, so continue to the next iteration.
- Step 5: If the string 's' does not contain any invalid digits, check if it contains at least one of the 'good' digits '2', '5', '6', or '9'.
- Step 6: If the string 's' contains at least one 'good' digit, increment the counter 'count'.
- Step 7: After iterating through all the numbers from 1 to 'n', return the value of 'count'.
Key Insights
- Insight 1: A number is invalid if it contains the digits 3, 4, or 7 because these digits do not have a valid rotated counterpart.
- Insight 2: A valid number is considered 'good' only if it contains at least one of the digits 2, 5, 6, or 9, as these digits change upon rotation.
- Insight 3: The problem can be efficiently solved by iterating through each number in the given range and checking if it satisfies both validity and 'goodness' criteria.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Math, Dynamic Programming.
Companies
Asked at: Arista Networks.