Welcome to our exploration of strobogrammatic numbers! In this post, we’ll dive into what these intriguing numbers are and how to write Java programs to check if a number is strobogrammatic and to generate strobogrammatic numbers of a specific length. Let’s start with the basics!
What is a Strobogrammatic Number?
A strobogrammatic number is a number that looks the same when rotated 180 degrees (upside down). Imagine flipping your calculator upside down; some numbers will still look like valid numbers! For example, 69 becomes 96 when flipped, and 88 stays 88. Cool, right?
Strobogrammatic Digits
Only certain digit pairs remain valid when flipped:
- 0 ↔ 0
- 1 ↔ 1
- 6 ↔ 9
- 8 ↔ 8
- 9 ↔ 6
These pairs will be our foundation for checking and generating strobogrammatic numbers.
Writing a Java Program to Check Strobogrammatic Numbers
Let’s write a Java program to determine if a given number is strobogrammatic.
Step-by-Step Logic
- Initialize Pointers: Start with pointers at the beginning and end of the number.
- Check Each Pair: Move pointers towards the center, checking if the current pair of digits matches the strobogrammatic condition.
- Return the Result: If any pair fails, the number is not strobogrammatic. If all pairs match, it is!
Here’s the Java code for this logic:
public class StrobogrammaticChecker {
public static boolean isStrobogrammatic(String num) {
char[] pairs = {'0', '1', '8', '6', '9'};
int left = 0, right = num.length() - 1;
while (left <= right) {
char l = num.charAt(left);
char r = num.charAt(right);
if ((l == '0' && r != '0') ||
(l == '1' && r != '1') ||
(l == '6' && r != '9') ||
(l == '8' && r != '8') ||
(l == '9' && r != '6')) {
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) {
System.out.println(isStrobogrammatic("69")); // true
System.out.println(isStrobogrammatic("88")); // true
System.out.println(isStrobogrammatic("962")); // false
}
}
How It Works
pairs
: Contains the valid strobogrammatic pairs.- Pointers
left
andright
: Traverse the number from both ends. - Conditions: Check if the left and right characters form a valid strobogrammatic pair. If not, return false.
Generating Strobogrammatic Numbers
Now, let’s create a Java program to generate strobogrammatic numbers of a given length.
Step-by-Step Logic
- Recursive Helper Function: Builds the number from the inside out using valid pairs.
- Base Cases: Handle 0-length and 1-length numbers separately.
- Avoid Leading Zeros: Do not add ‘0’ at the outermost positions except for the number ‘0’ itself.
Here’s the Java code:
import java.util.ArrayList;
import java.util.List;
public class StrobogrammaticGenerator {
public static List<String> generateStrobogrammatic(int n) {
return helper(n, n);
}
private static List<String> helper(int n, int finalLength) {
if (n == 0) return new ArrayList<>(List.of(""));
if (n == 1) return new ArrayList<>(List.of("0", "1", "8"));
List<String> middles = helper(n - 2, finalLength);
List<String> results = new ArrayList<>();
for (String middle : middles) {
if (n != finalLength) {
results.add("0" + middle + "0");
}
results.add("1" + middle + "1");
results.add("6" + middle + "9");
results.add("8" + middle + "8");
results.add("9" + middle + "6");
}
return results;
}
public static void main(String[] args) {
System.out.println(generateStrobogrammatic(2));
System.out.println(generateStrobogrammatic(3));
}
}
How It Works
helper
Function: Recursively constructs numbers by adding valid pairs around the middle part.- Base Cases: Return simple strobogrammatic numbers for lengths 0 and 1.
- Avoid Leading Zeros: Only add ‘0’ at the outermost positions if it’s not the final outermost layer.
Summary
In this post, we covered how to:
- Check if a number is strobogrammatic using a simple two-pointer technique.
- Generate strobogrammatic numbers of a given length with recursion.
Understanding these concepts will sharpen your problem-solving skills in Java and broaden your knowledge of number theory. Keep experimenting with variations, and happy coding!
Feel free to ask questions or share your own experiences with strobogrammatic numbers in the comments below!
Tags: #Java #Programming #Beginner #StrobogrammaticNumbers #Coding