248. Strobogrammatic Number III
HardSolutions 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
A strobogrammatic number is a number that appears the same when rotated 180 degrees. The problem asks us to count how many such numbers exist within a given string-represented integer range [low, high].
Solution
Strobogrammatic Number III in Python
class Solution:
def strobogrammaticInRange(self, low: str, high: str) -> int:
self.count = 0
self.low = low
self.high = high
self.pairs = [('0', '0'), ('1', '1'), ('6', '9'), ('8', '8'), ('9', '6')]
self.singles = ['0', '1', '8']
# Iterate through all possible lengths from low's length to high's length
for length in range(len(low), len(high) + 1):
self.dfs([''] * length, 0, length - 1)
return self.count
def dfs(self, current_num: list[str], left: int, right: int):
# Base case: the number is fully constructed
if left > right:
s = "".join(current_num)
# Check if the generated number is within the [low, high] range
if (len(s) == len(self.low) and s < self.low) or \
(len(s) == len(self.high) and s > self.high):
return
self.count += 1
return
# If at the middle of an odd-length number
if left == right:
for digit in self.singles:
current_num[left] = digit
self.dfs(current_num, left + 1, right - 1)
else:
# Place pairs at the current left and right positions
for p1, p2 in self.pairs:
# Avoid leading zero if number length is greater than 1
if left == 0 and p1 == '0' and len(current_num) > 1:
continue
current_num[left], current_num[right] = p1, p2
self.dfs(current_num, left + 1, right - 1)
Strobogrammatic Number III in Java
class Solution {
private int count = 0;
private String low, high;
// Pairs of strobogrammatic digits
private final char[][] pairs = {{'0','0'}, {'1','1'}, {'6','9'}, {'8','8'}, {'9','6'}};
// Single strobogrammatic digits for the middle of odd-length numbers
private final char[] singles = {'0', '1', '8'};
public int strobogrammaticInRange(String low, String high) {
this.low = low;
this.high = high;
// Iterate through all possible lengths from low.length() to high.length()
for (int len = low.length(); len <= high.length(); len++) {
char[] current = new char[len];
dfs(current, 0, len - 1);
}
return count;
}
private void dfs(char[] current, int left, int right) {
// Base case: the number is fully constructed
if (left > right) {
String s = new String(current);
// Check if the generated number is within the [low, high] range
if ((current.length == low.length() && s.compareTo(low) < 0) ||
(current.length == high.length() && s.compareTo(high) > 0)) {
return;
}
count++;
return;
}
// If at the middle of an odd-length number
if (left == right) {
for (char c : singles) {
current[left] = c;
dfs(current, left + 1, right - 1);
}
} else {
// Place pairs at the current left and right positions
for (char[] p : pairs) {
// Avoid leading zero if number length is greater than 1
if (left == 0 && p[0] == '0' && current.length > 1) {
continue;
}
current[left] = p[0];
current[right] = p[1];
dfs(current, left + 1, right - 1);
}
}
}
}
Strobogrammatic Number III in C++
class Solution {
public:
int strobogrammaticInRange(string low, string high) {
this->low_str = low;
this->high_str = high;
// Iterate through all possible lengths from low.length() to high.length()
for (int len = low.length(); len <= high.length(); ++len) {
string current(len, ' ');
dfs(current, 0, len - 1);
}
return count;
}
private:
int count = 0;
string low_str, high_str;
// Pairs of strobogrammatic digits
const vector<pair<char, char>> pairs = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
// Single strobogrammatic digits for the middle of odd-length numbers
const vector<char> singles = {'0', '1', '8'};
void dfs(string& current, int left, int right) {
// Base case: the number is fully constructed
if (left > right) {
// Check if the generated number is within the [low, high] range
if (current.length() == low_str.length() && current < low_str) {
return;
}
if (current.length() == high_str.length() && current > high_str) {
return;
}
count++;
return;
}
// If at the middle of an odd-length number
if (left == right) {
for (char c : singles) {
current[left] = c;
dfs(current, left + 1, right - 1);
}
} else {
// Place pairs at the current left and right positions
for (auto const& p : pairs) {
// Avoid leading zero if number length is greater than 1
if (left == 0 && p.first == '0' && current.length() > 1) {
continue;
}
current[left] = p.first;
current[right] = p.second;
dfs(current, left + 1, right - 1);
}
}
}
};
Strobogrammatic Number III in TypeScript
function strobogrammaticInRange(low: string, high: string): number {
let count = 0;
const pairs = [['0', '0'], ['1', '1'], ['6', '9'], ['8', '8'], ['9', '6']];
const singles = ['0', '1', '8'];
const dfs = (current: string[], left: number, right: number) => {
// Base case: the number is fully constructed
if (left > right) {
const s = current.join('');
// Check if the generated number is within the [low, high] range
if ((s.length === low.length && s < low) ||
(s.length === high.length && s > high)) {
return;
}
count++;
return;
}
// If at the middle of an odd-length number
if (left === right) {
for (const c of singles) {
current[left] = c;
dfs(current, left + 1, right - 1);
}
} else {
// Place pairs at the current left and right positions
for (const p of pairs) {
// Avoid leading zero if number length is greater than 1
if (left === 0 && p[0] === '0' && current.length > 1) {
continue;
}
current[left] = p[0];
current[right] = p[1];
dfs(current, left + 1, right - 1);
}
}
};
// Iterate through all possible lengths from low.length to high.length
for (let len = low.length; len <= high.length; len++) {
const current = new Array<string>(len);
dfs(current, 0, len - 1);
}
return count;
};
More Hard 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.