|
| 1 | +package LeetCodeJava.DFS; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/strobogrammatic-number-ii/description/ |
| 4 | +// https://leetcode.ca/all/247.html |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +/** |
| 9 | + * 247. Strobogrammatic Number II |
| 10 | + * A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). |
| 11 | + * |
| 12 | + * Find all strobogrammatic numbers that are of length = n. |
| 13 | + * |
| 14 | + * Example: |
| 15 | + * |
| 16 | + * Input: n = 2 |
| 17 | + * Output: ["11","69","88","96"] |
| 18 | + * Difficulty: |
| 19 | + * Medium |
| 20 | + * Lock: |
| 21 | + * Prime |
| 22 | + * Company: |
| 23 | + * Cisco Facebook Google |
| 24 | + * Problem Solution |
| 25 | + * 247-Strobogrammatic-Number-II |
| 26 | + * |
| 27 | + */ |
| 28 | +public class StrobogrammaticNumber2 { |
| 29 | + |
| 30 | + // V0 |
| 31 | + // TODO: validate and fix |
| 32 | +// List<String> res = new ArrayList<>(); |
| 33 | +// public List<String> findStrobogrammatic_1_1(int n) { |
| 34 | +// for(int i = 0; i < n; i++){ |
| 35 | +// HashSet<String> cache = new HashSet<>(); |
| 36 | +// StringBuilder sb = new StringBuilder(); |
| 37 | +// findNumbers(n, sb, cache); |
| 38 | +// } |
| 39 | +// return res; |
| 40 | +// } |
| 41 | +// |
| 42 | +// private void findNumbers(int n, StringBuilder sb, Set<String> cache){ |
| 43 | +// |
| 44 | +// String[] digits = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; |
| 45 | +// String str = sb.toString(); |
| 46 | +// if (str.length() == n){ |
| 47 | +// if (isStrobogrammatic(str)){ |
| 48 | +// if(!cache.contains(str)){ |
| 49 | +// cache.add(str); |
| 50 | +// res.add(str); |
| 51 | +// } |
| 52 | +// } |
| 53 | +// return; |
| 54 | +// } |
| 55 | +// |
| 56 | +// if (str.length() > n){ |
| 57 | +// return; |
| 58 | +// } |
| 59 | +// |
| 60 | +// for (String x: digits){ |
| 61 | +// findNumbers(n, sb.append(x), cache); |
| 62 | +// } |
| 63 | +// } |
| 64 | +// |
| 65 | +// private boolean isStrobogrammatic(String x){ |
| 66 | +// Map<String, String> symmertricMapping = new HashMap<>(); |
| 67 | +// symmertricMapping.put("0", "0"); |
| 68 | +// symmertricMapping.put("1", "1"); |
| 69 | +// symmertricMapping.put("8", "8"); |
| 70 | +// symmertricMapping.put("6", "9"); |
| 71 | +// symmertricMapping.put("9", "6"); |
| 72 | +// int l = 0; |
| 73 | +// int r = x.length() - 1; |
| 74 | +// while (r > l){ |
| 75 | +//// if (x.charAt(l) != x.charAt(r)){ |
| 76 | +//// return false; |
| 77 | +//// } |
| 78 | +// String left = String.valueOf(x.charAt(l)); |
| 79 | +// String right = String.valueOf(x.charAt(r)); |
| 80 | +// if (!symmertricMapping.get(left).equals(right)){ |
| 81 | +// return false; |
| 82 | +// } |
| 83 | +// r -= 1; |
| 84 | +// l += 1; |
| 85 | +// } |
| 86 | +// |
| 87 | +// return true; |
| 88 | +// } |
| 89 | + |
| 90 | + // V1-1 |
| 91 | + // https://leetcode.ca/2016-08-03-247-Strobogrammatic-Number-II/ |
| 92 | + List<String> singleDigitList = new ArrayList<>(Arrays.asList("0", "1", "8")); // not char[], because List can direct return as result |
| 93 | + char[][] digitPair = { {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'} }; // except '0', a special case |
| 94 | + |
| 95 | + public List<String> findStrobogrammatic_1_1(int n) { |
| 96 | + return dfs(n, n); |
| 97 | + } |
| 98 | + |
| 99 | + public List<String> dfs(int k, int n) { |
| 100 | + if (k <= 0) { |
| 101 | + return new ArrayList<String>(Arrays.asList("")); |
| 102 | + } |
| 103 | + if (k == 1) { |
| 104 | + return singleDigitList; |
| 105 | + } |
| 106 | + |
| 107 | + List<String> subList = dfs(k - 2, n); |
| 108 | + List<String> result = new ArrayList<>(); |
| 109 | + |
| 110 | + for (String str : subList) { |
| 111 | + if (k != n) { // @note: cannot start with 0 |
| 112 | + result.add("0" + str + "0"); |
| 113 | + } |
| 114 | + for (char[] aDigitPair : digitPair) { |
| 115 | + result.add(aDigitPair[0] + str + aDigitPair[1]); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + // V1-2 |
| 123 | + // https://leetcode.ca/2016-08-03-247-Strobogrammatic-Number-II/ |
| 124 | + private static final int[][] PAIRS = { {1, 1}, {8, 8}, {6, 9}, {9, 6}}; |
| 125 | + private int n; |
| 126 | + |
| 127 | + public List<String> findStrobogrammatic_1_2(int n) { |
| 128 | + this.n = n; |
| 129 | + return dfs(n); |
| 130 | + } |
| 131 | + |
| 132 | + private List<String> dfs(int u) { |
| 133 | + if (u == 0) { |
| 134 | + return Collections.singletonList(""); |
| 135 | + } |
| 136 | + if (u == 1) { |
| 137 | + return Arrays.asList("0", "1", "8"); |
| 138 | + } |
| 139 | + List<String> ans = new ArrayList<>(); |
| 140 | + for (String v : dfs(u - 2)) { |
| 141 | + for ( int[] p : PAIRS) { |
| 142 | + ans.add(p[0] + v + p[1]); |
| 143 | + } |
| 144 | + if (u != n) { |
| 145 | + ans.add(0 + v + 0); |
| 146 | + } |
| 147 | + } |
| 148 | + return ans; |
| 149 | + } |
| 150 | + |
| 151 | + // V2-1 |
| 152 | + // IDEA : DFS (gpt) |
| 153 | + // TODO: validate the code |
| 154 | + List<String> res = new ArrayList<>(); |
| 155 | + public List<String> findStrobogrammatic_2_1(int n) { |
| 156 | + // Use valid strobogrammatic pairs only |
| 157 | + char[][] pairs = new char[][] { |
| 158 | + {'0', '0'}, {'1', '1'}, {'8', '8'}, |
| 159 | + {'6', '9'}, {'9', '6'} |
| 160 | + }; |
| 161 | + |
| 162 | + findNumbers(n, new char[n], 0, n - 1, pairs); |
| 163 | + return res; |
| 164 | + } |
| 165 | + |
| 166 | + private void findNumbers(int n, char[] current, int left, int right, char[][] pairs) { |
| 167 | + if (left > right) { |
| 168 | + // Base case: Valid strobogrammatic number |
| 169 | + res.add(new String(current)); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + for (char[] pair : pairs) { |
| 174 | + // Avoid leading zero unless the number is of length 1 |
| 175 | + if (left == 0 && pair[0] == '0' && n > 1) { |
| 176 | + continue; |
| 177 | + } |
| 178 | + |
| 179 | + // Place the pair |
| 180 | + current[left] = pair[0]; |
| 181 | + current[right] = pair[1]; |
| 182 | + |
| 183 | + // Recursive call |
| 184 | + findNumbers(n, current, left + 1, right - 1, pairs); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments