|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). |
| 8 | +
|
| 9 | +Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. |
| 10 | +
|
| 11 | +For example, |
| 12 | +Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers. |
| 13 | +
|
| 14 | +Note: |
| 15 | +Because the range might be a large number, the low and high numbers are represented as string. |
| 16 | +
|
| 17 | +*/ |
| 18 | +public class _248 { |
| 19 | + |
| 20 | + static class Solution2 { |
| 21 | + /**Credit: https://discuss.leetcode.com/topic/31386/concise-java-solution |
| 22 | + * |
| 23 | + Construct char arrays from low.length() to high.length() |
| 24 | + Add stro pairs from outside |
| 25 | + When left > right, add eligible count |
| 26 | + */ |
| 27 | + |
| 28 | + private static final char[][] pairs = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}}; |
| 29 | + |
| 30 | + public int strobogrammaticInRange(String low, String high) { |
| 31 | + int[] count = {0}; |
| 32 | + for (int len = low.length(); len <= high.length(); len++) { |
| 33 | + char[] c = new char[len]; |
| 34 | + dfs(low, high, c, 0, len - 1, count); |
| 35 | + } |
| 36 | + return count[0]; |
| 37 | + } |
| 38 | + |
| 39 | + public void dfs(String low, String high , char[] c, int left, int right, int[] count) { |
| 40 | + if (left > right) { |
| 41 | + String s = new String(c); |
| 42 | + if ((s.length() == low.length() && s.compareTo(low) < 0) || |
| 43 | + (s.length() == high.length() && s.compareTo(high) > 0)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + count[0]++; |
| 47 | + return; |
| 48 | + } |
| 49 | + for (char[] p : pairs) { |
| 50 | + c[left] = p[0]; |
| 51 | + c[right] = p[1]; |
| 52 | + if (c.length != 1 && c[0] == '0') { |
| 53 | + continue; |
| 54 | + } |
| 55 | + if (left == right && p[0] != p[1]) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + dfs(low, high, c, left + 1, right - 1, count); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + class Solution1 { |
| 64 | + Map<Character, Character> map = new HashMap<>(); |
| 65 | + |
| 66 | + { |
| 67 | + map.put('1', '1'); |
| 68 | + map.put('8', '8'); |
| 69 | + map.put('6', '9'); |
| 70 | + map.put('9', '6'); |
| 71 | + map.put('0', '0'); |
| 72 | + } |
| 73 | + |
| 74 | + String low = "", high = ""; |
| 75 | + |
| 76 | + public int strobogrammaticInRange(String low, String high) { |
| 77 | + this.low = low; |
| 78 | + this.high = high; |
| 79 | + int result = 0; |
| 80 | + for (int n = low.length(); n <= high.length(); n++) { |
| 81 | + int[] count = new int[1]; |
| 82 | + strobogrammaticInRange(new char[n], count, 0, n - 1); |
| 83 | + result += count[0]; |
| 84 | + } |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + private void strobogrammaticInRange(char[] arr, int[] count, int lo, int hi) { |
| 89 | + if (lo > hi) { |
| 90 | + String s = new String(arr); |
| 91 | + if ((arr[0] != '0' || arr.length == 1) && compare(low, s) && compare(s, high)) { |
| 92 | + count[0]++; |
| 93 | + } |
| 94 | + return; |
| 95 | + } |
| 96 | + for (Character c : map.keySet()) { |
| 97 | + arr[lo] = c; |
| 98 | + arr[hi] = map.get(c); |
| 99 | + if ((lo == hi && c == map.get(c)) || lo < hi) |
| 100 | + strobogrammaticInRange(arr, count, lo + 1, hi - 1); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private boolean compare(String a, String b) { |
| 105 | + if (a.length() != b.length()) |
| 106 | + return a.length() < b.length(); |
| 107 | + int i = 0; |
| 108 | + while (i < a.length() && a.charAt(i) == b.charAt(i)) |
| 109 | + i++; |
| 110 | + return i == a.length() ? true : a.charAt(i) <= b.charAt(i); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments