Advertisement
1988coder

248. Strobogrammatic Number III

Jan 21st, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/strobogrammatic-number-iii/
  2.  
  3. /**
  4.  * Construct strobogrammatic numbers from low.length() to high.length()
  5.  *
  6.  * Time Complexity: O(l Sum H (5 ^ (i/2))) = O(5^(H/2) - 5^(L/2))
  7.  *
  8.  * Space Complexity: O(H)
  9.  *
  10.  * L = length of low string. H = length of high string.
  11.  */
  12. class Solution {
  13.     private static final char[][] PAIRS = new char[][] { { '0', '0' }, { '1', '1' }, { '8', '8' }, { '6', '9' },
  14.             { '9', '6' } };
  15.  
  16.     public int strobogrammaticInRange(String low, String high) {
  17.         if (low == null || high == null) {
  18.             return 0;
  19.         }
  20.  
  21.         // Below section of code removes the leading zeros from low and high input
  22.         // strings. Only write this is this case can happen.
  23.         // ---------------------------------------------------------------------
  24.         int i = 0;
  25.         while (i < low.length() && low.charAt(i) == '0') {
  26.             i++;
  27.         }
  28.         if (i == low.length()) {
  29.             low = "0";
  30.         } else {
  31.             low = low.substring(i);
  32.         }
  33.         i = 0;
  34.         while (i < high.length() && high.charAt(i) == '0') {
  35.             i++;
  36.         }
  37.         if (i == high.length()) {
  38.             high = "0";
  39.         } else {
  40.             high = high.substring(i);
  41.         }
  42.         // ---------------------------------------------------------------------
  43.  
  44.         if (low.length() > high.length() || (low.length() == high.length() && low.compareTo(high) > 0)) {
  45.             return 0;
  46.         }
  47.  
  48.         int count = 0;
  49.         for (int len = low.length(); len <= high.length(); len++) {
  50.             count += helper(low, high, new char[len], 0, len - 1);
  51.         }
  52.         return count;
  53.     }
  54.  
  55.     private int helper(String low, String high, char[] chArr, int left, int right) {
  56.         if (left > right) {
  57.             String s = new String(chArr);
  58.             if ((s.length() == low.length() && s.compareTo(low) < 0)
  59.                     || (s.length() == high.length() && s.compareTo(high) > 0)) {
  60.                 return 0;
  61.             } else {
  62.                 return 1;
  63.             }
  64.         }
  65.         int cnt = 0;
  66.         for (char[] p : PAIRS) {
  67.             if (chArr.length > 1 && left == 0 && p[0] == '0') {
  68.                 continue;
  69.             }
  70.             if (left == right && p[0] != p[1]) {
  71.                 continue;
  72.             }
  73.             chArr[left] = p[0];
  74.             chArr[right] = p[1];
  75.             cnt += helper(low, high, chArr, left + 1, right - 1);
  76.         }
  77.         return cnt;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement