w3resource

Java: Check if a number is a strobogrammatic number

Java Basic: Exercise-186 with Solution

Write a Java program to check if a number is a strobogrammatic number. The number is represented as a string.

According to Wikipedia "A strobogrammatic number is a number whose numeral is rotationally symmetric, so that it appears the same when rotated 180 degrees. In other words, the numeral looks the same right-side up and upside down (e.g., 69, 96, 1001). A strobogrammatic prime is a strobogrammatic number that is also a prime number, i.e., a number that is only divisible by one and itself (e.g., 11). It is a type of ambigram, words and numbers that retain their meaning when viewed from a different perspective, such as palindromes."
The first few strobogrammatic numbers are:
0, 1, 8, 11, 69, 88, 96, 101, 111, 181, 609, 619, 689, 808, 818, 888, 906, 916, 986, 1001, 1111, 1691, 1881, 1961, 6009, 6119, 6699, 6889, 6969, 8008, 8118, 8698, 8888, 8968, 9006, 9116, 9696, 9886, 9966, ...

Visual Presentation:

Java Basic Exercises: Check if a number is a strobogrammatic number.

Sample Solution:

Java Code:

// Importing necessary Java utilities
import java.util.*;
// Main class 
public class Main {
    // Main method
    public static void main(String[] args) {
        // Declaring and initializing a string
        String n = "9006";
        // Printing if the string is Strobogrammatic or not
        System.out.println("Is " + n + " is Strobogrammatic? " + is_Strobogrammatic(n));
    }
    // Method to check if the given string is Strobogrammatic
    public static boolean is_Strobogrammatic(String n) {
        // Check for null or empty string
        if (n == null || n.length() == 0) {
            return true;
        }
        // Create a HashMap to store Strobogrammatic pairs
        Map<Character, Character> map = new HashMap<>();
        map.put('0', '0');
        map.put('1', '1');
        map.put('8', '8');
        map.put('6', '9');
        map.put('9', '6');
        // Use two pointers to traverse the string from both ends
        int left = 0;
        int right = n.length() - 1;
        // Continue until the left pointer is less than or equal to the right pointer
        while (left <= right) {
            // Check if the characters at the current positions are valid Strobogrammatic pairs
            if (!map.containsKey(n.charAt(right)) || n.charAt(left) != map.get(n.charAt(right))) {
                return false;
            }
            // Move the pointers towards the center
            left++;
            right--;
        }
        // If the loop completes, the string is Strobogrammatic
        return true;
    }
}

Sample Output:

Is 9006 is Strobogrammatic? true

Flowchart:

Flowchart: Java exercises: Check if two specified strings  are isomorphic or not.

Java Code Editor:

Company:  Google

Contribute your code and comments through Disqus.

Previous: Write a Java program to check if two given strings are isomorphic or not.
Next: Write a Java program to find the index of first non-repeating character in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.