Similar Problems

Similar Problems not available

Rotated Digits - Leetcode Solution

Companies:

LeetCode:  Rotated Digits Leetcode Solution

Difficulty: Medium

Topics: math dynamic-programming  

The Rotated Digits problem on LeetCode asks us to find out the number of good numbers in a given range [1, N] where a good number is defined as a number which:

  1. Consists only of the digits 0, 1, 2, 5, 6, 8, 9.
  2. When we rotate each digit by 180 degrees, we get a different number. For example, 2 and 5 are good numbers because when we rotate them, we get 5 and 2 respectively, which are different from the original numbers.

To solve this problem, we can use a brute-force approach where we iterate over all the numbers in the given range and check if each number is a good number or not. If a number is a good number, we increment a counter variable.

To check if a number is a good number, we can first check if the number contains any of the digits 3, 4, or 7 because these digits cannot be rotated into a different number. If the number contains any of these digits, we can skip this number and move on to the next number.

If the number does not contain any of the digits 3, 4, or 7, then we need to check if the number contains any of the digits 2, 5, 6, 9 because these digits can be rotated into a different number. If the number does not contain any of these digits, then we can skip this number because it cannot be a good number.

If the number contains any of the digits 2, 5, 6, 9, then we need to rotate each digit of the number and check if the rotated number is different from the original number. If the rotated number is different from the original number, then the number is a good number.

The following is the Python code for the solution:

class Solution:
    def rotatedDigits(self, N: int) -> int:
        good_numbers = 0
        for i in range(1, N+1):
            if any(x in str(i) for x in ['3', '4', '7']):
                continue
            if not any(x in str(i) for x in ['2', '5', '6', '9']):
                continue
            rotated_number = int(str(i).translate(str.maketrans('2569', '9526')))
            if rotated_number != i:
                good_numbers += 1
        return good_numbers

In this code, we iterate over all the numbers in the range [1, N] using a for loop. For each number, we check if it contains any of the digits 3, 4, or 7 using the any() function and the in keyword. If the number contains any of these digits, we skip this number using the continue keyword and move on to the next number.

If the number does not contain any of the digits 3, 4, or 7, we check if it contains any of the digits 2, 5, 6, or 9 using the any() function and the in keyword. If the number does not contain any of these digits, we skip this number using the continue keyword and move on to the next number.

If the number contains any of the digits 2, 5, 6, or 9, we rotate each digit of the number using the translate() function and the str.maketrans() method. We then convert the rotated number back to an integer using the int() function and check if it is different from the original number. If the rotated number is different from the original number, we increment the good_numbers counter variable using the += operator.

Finally, we return the number of good numbers.

Rotated Digits Solution Code

1