804. Rotated Digits
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to count the number of "good" integers from 1 to n. A number is defined as "good" if, after rotating each of its digits 180 degrees, it results in a new, valid number that is different from the original.
Solution
Rotated Digits in Python
class Solution:
def rotatedDigits(self, n: int) -> int:
count = 0
# Iterate through each number from 1 to n
for i in range(1, n + 1):
s = str(i)
# A number is invalid if it contains any digit from {'3', '4', '7'}
# because they don't rotate to a valid digit.
if any(c in '347' for c in s):
continue
# A number is "good" if it's valid (checked above) and
# it contains at least one digit from {'2', '5', '6', '9'}.
# This ensures the rotated number is different from the original.
if any(c in '2569' for c in s):
count += 1
return count
Rotated Digits in Java
class Solution {
public int rotatedDigits(int n) {
int count = 0;
// Iterate through each number from 1 to n
for (int i = 1; i <= n; i++) {
if (isGood(i)) {
count++;
}
}
return count;
}
// Helper function to check if a number is "good"
private boolean isGood(int num) {
boolean hasChangingDigit = false;
int temp = num;
while (temp > 0) {
int digit = temp % 10;
// Digits 3, 4, 7 are invalid after rotation
if (digit == 3 || digit == 4 || digit == 7) {
return false;
}
// Digits 2, 5, 6, 9 change after rotation
if (digit == 2 || digit == 5 || digit == 6 || digit == 9) {
hasChangingDigit = true;
}
temp /= 10;
}
// A number is "good" if it's valid (no 3,4,7) and changes (has 2,5,6,9)
return hasChangingDigit;
}
}
Rotated Digits in C++
class Solution {
public:
int rotatedDigits(int n) {
int count = 0;
// Iterate through each number from 1 to n
for (int i = 1; i <= n; ++i) {
if (isGood(i)) {
count++;
}
}
return count;
}
private:
// Helper function to check if a number is "good"
bool isGood(int num) {
bool hasChangingDigit = false;
int temp = num;
while (temp > 0) {
int digit = temp % 10;
// Digits 3, 4, 7 are invalid after rotation
if (digit == 3 || digit == 4 || digit == 7) {
return false;
}
// Digits 2, 5, 6, 9 change after rotation
if (digit == 2 || digit == 5 || digit == 6 || digit == 9) {
hasChangingDigit = true;
}
temp /= 10;
}
// A number is "good" if it's valid (no 3,4,7) and changes (has 2,5,6,9)
return hasChangingDigit;
}
};
Rotated Digits in TypeScript
function rotatedDigits(n: number): number {
let count = 0;
// Iterate through each number from 1 to n
for (let i = 1; i <= n; i++) {
const s = String(i);
// A number is invalid if it contains 3, 4, or 7.
// We use a regular expression to test for their presence.
if (/[347]/.test(s)) {
continue;
}
// A number is "good" if it's valid (checked above) and
// it contains at least one digit from {2, 5, 6, 9}.
// This ensures the rotated number is different from the original.
if (/[2569]/.test(s)) {
count++;
}
}
return count;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.