Leetcode 788: Rotated Digits

grid47
grid47
Exploring patterns and algorithms
Aug 20, 2024 6 min read

A set of digits where the rotated digits are found, glowing softly as the valid rotations are made.
Solution to LeetCode 788: Rotated Digits Problem

Given an integer n, find how many integers in the range [1, n] are ‘good’ integers. A number is considered ‘good’ if after rotating each of its digits by 180 degrees, it becomes a valid different number. The rotation rules for digits are as follows: 0, 1, and 8 stay the same, 2 and 5 swap places, 6 and 9 swap places, and other digits are not valid when rotated.
Problem
Approach
Steps
Complexity
Input: You are given an integer n representing the upper bound of the range. You need to check for all integers from 1 to n which are 'good'.
Example: n = 15
Constraints:
• 1 <= n <= 10^4
Output: Return the count of 'good' integers between 1 and n, inclusive.
Example: Output: 6
Constraints:
• The output will be a non-negative integer.
Goal: To determine how many integers in the range [1, n] are 'good' by rotating their digits and ensuring the result is a valid, different number.
Steps:
• For each number in the range [1, n], break the number into its individual digits.
• For each digit, check if its rotation results in a valid number (refer to the rotation rules).
• If the number becomes valid and different from the original, count it as 'good'.
Goal: Ensure that the input is within the bounds specified by the constraints.
Steps:
• n must be between 1 and 10^4.
• Digits other than 0, 1, 2, 5, 6, 8, 9 are invalid when rotated.
Assumptions:
• It is assumed that the input number is valid and within the specified range.
Input: Input: n = 10
Explanation: The integers from 1 to 10 include 2, 5, 6, and 9, which are the 'good' numbers. Other numbers remain unchanged after rotation.

Input: Input: n = 1
Explanation: The only integer in the range is 1, which remains unchanged after rotation. Therefore, the count of 'good' integers is 0.

Input: Input: n = 15
Explanation: The 'good' integers in this range are 2, 5, 6, 9, 10, 11, 12, 15. Therefore, the output will be 6.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus