diff --git a/README.md b/README.md index cd92c21..6969b17 100644 --- a/README.md +++ b/README.md @@ -323,6 +323,7 @@ You can contribute to any open source project hosted on Github.com and contribut | Two Sum | :white_check_mark: | :white_check_mark: | | | | | | | Variations of LIS | | | | | | | | | Word Wrap Problem | | | | | | | | +| Strobogrammatic number | | :white_check_mark: | | | | | | ## Data Structures Implementations. diff --git a/others/cpp/strobogrammatic.java b/others/cpp/strobogrammatic.java new file mode 100644 index 0000000..8477332 --- /dev/null +++ b/others/cpp/strobogrammatic.java @@ -0,0 +1,32 @@ +import java.util.Scanner; +class strobogrammatic { + public static boolean isStrobogrammatic(char[] digits) { + int left = 0; + int right = digits.length - 1; + + while (left <= right) { + char c1 = digits[left]; + char c2 = digits[right]; + if (c1 == '0' && c2 == '0' || c1 == '1' && c2 == '1' || c1 == '8' && c2 == '8' || c1 == '6' && c2 == '9' || c1 == '9' && c2 == '6') { + left++; + right--; + } else { + return false; + } + } + + return true; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int num = sc.nextInt(); + char[] digits = String.valueOf(num).toCharArray(); + + if (isStrobogrammatic(digits)) { + System.out.println("Strobogrammatic"); + } else { + System.out.println("Not Strobogrammatic"); + } + } +}