Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. class Solution {
  2.     public int strobogrammaticInRange(String low, String high) {
  3.         int count = 0;
  4.         for(int i = low.length(); i <= high.length(); i++){
  5.             if(i%2 == 0){
  6.                 count += solve(low, high, "", i);
  7.             }else{
  8.                 count += solve(low, high, "0", i);
  9.                 count += solve(low, high, "1", i);
  10.                 count += solve(low, high, "8", i);
  11.             }
  12.         }
  13.         return count;
  14.     }
  15.     int solve(String low, String high, String cur, int len){
  16.         if(cur.length() == len){
  17.             if((low.length() == len && low.compareTo(cur) > 0)
  18.                || ((high.length() == len && high.compareTo(cur) < 0))) return 0;
  19.             return 1;
  20.         }
  21.         int count = 0;
  22.         if(cur.length() + 2 < len) count += solve(low, high, "0" + cur + "0", len);
  23.         count += solve(low, high, "8" + cur + "8", len);
  24.         count += solve(low, high, "1" + cur + "1", len);
  25.         count += solve(low, high, "9" + cur + "6", len);
  26.         count += solve(low, high, "6" + cur + "9", len);
  27.         return count;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement