package com.fishercoder.solutions;

import java.util.HashMap;
import java.util.Map;

/**
 * 788. Rotated Digits
 *
 * X is a good number if after rotating each digit individually by 180 degrees,
 * we get a valid number that is different from X.
 * A number is valid if each digit remains a digit after rotation.
 * 0, 1, and 8 rotate to themselves;
 * 2 and 5 rotate to each other;
 * 6 and 9 rotate to each other,
 * and the rest of the numbers do not rotate to any other number.

 Now given a positive number N, how many numbers X from 1 to N are good?

 Example:
 Input: 10
 Output: 4

 Explanation:
 There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
 Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.

 Note: N  will be in range [1, 10000].
 */
public class _788 {
  public static class Solution1 {
    public int rotatedDigits(int N) {
      int count = 0;
      Map<Character, String> map = new HashMap<>();
      map.put('0', "0");
      map.put('1', "1");
      map.put('8', "8");
      map.put('2', "5");
      map.put('5', "2");
      map.put('6', "9");
      map.put('9', "6");
      for (int i = 1; i <= N; i++) {
        if (isRotatedNumber(i, map)) {
          count++;
        }
      }
      return count;
    }

    private boolean isRotatedNumber(int num, Map<Character, String> map) {
      String originalNum = String.valueOf(num);
      StringBuilder sb = new StringBuilder();
      for (char c : String.valueOf(num).toCharArray()) {
        if (!map.containsKey(c)) {
          return false;
        } else {
          sb.append(map.get(c));
        }
      }
      return !originalNum.equals(sb.toString());
    }
  }
}

@@ -82,721 +82,8 @@
 p;%0A%0A
-/**%0A * 788. Rotated Digits%0A *%0A * X is a good number if after rotating each digit individually by 180 degrees,%0A * we get a valid number that is different from X.%0A * A number is valid if each digit remains a digit after rotation.%0A * 0, 1, and 8 rotate to themselves;%0A * 2 and 5 rotate to each other;%0A * 6 and 9 rotate to each other,%0A * and the rest of the numbers do not rotate to any other number.%0A%0A Now given a positive number N, how many numbers X from 1 to N are good?%0A%0A Example:%0A Input: 10%0A Output: 4%0A%0A Explanation:%0A There are four good numbers in the range %5B1, 10%5D : 2, 5, 6, 9.%0A Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.%0A%0A Note: N  will be in range %5B1, 10000%5D.%0A */%0A
 publ
@@ -98,16 +98,18 @@
  _788 %7B%0A
+  
   public
@@ -134,16 +134,20 @@
 tion1 %7B%0A
+    
     publ
@@ -182,16 +182,22 @@
 %7B%0A      
+   
+   
 int coun
@@ -203,16 +203,22 @@
 nt = 0;%0A
+      
       Ma
@@ -259,32 +259,38 @@
 hMap%3C%3E();%0A      
+     
+ 
 map.put('0', %220%22
@@ -288,24 +288,30 @@
 ('0', %220%22);%0A
+      
       map.pu
@@ -319,24 +319,30 @@
 ('1', %221%22);%0A
+      
       map.pu
@@ -352,32 +352,38 @@
 8', %228%22);%0A      
+ 
+     
 map.put('2', %225%22
@@ -383,32 +383,38 @@
 2', %225%22);%0A      
+  
+    
 map.put('5', %222%22
@@ -414,32 +414,38 @@
 5', %222%22);%0A      
+   
+   
 map.put('6', %229%22
@@ -449,24 +449,30 @@
 %229%22);%0A      
+    
+  
 map.put('9',
@@ -480,24 +480,30 @@
 %226%22);%0A      
+     
+ 
 for (int i =
@@ -525,24 +525,32 @@
 ) %7B%0A        
+      
+  
 if (isRotate
@@ -578,16 +578,26 @@
         
+     
+     
 count++;
@@ -601,32 +601,54 @@
 ++;%0A        
+        %7D%0A            
 %7D%0A      
 %7D%0A      retu
@@ -627,34 +627,32 @@
         %7D%0A      
-%7D%0A
       return cou
@@ -659,15 +659,23 @@
 nt;%0A
+    
     %7D%0A%0A
+    
     
@@ -741,24 +741,30 @@
 ing%3E map) %7B%0A
+      
       String
@@ -801,24 +801,30 @@
 num);%0A      
+    
+  
 StringBuilde
@@ -847,24 +847,30 @@
 gBuilder();%0A
+      
       for (c
@@ -918,24 +918,32 @@
 ) %7B%0A        
+      
+  
 if (!map.con
@@ -953,24 +953,34 @@
 nsKey(c)) %7B%0A
+          
           re
@@ -999,16 +999,24 @@
         
+  
+      
 %7D else %7B
@@ -1026,16 +1026,26 @@
         
+        
+  
 sb.appen
@@ -1067,24 +1067,46 @@
         
+        %7D%0A            
 %7D%0A      
 %7D%0A      
@@ -1093,26 +1093,24 @@
     %7D%0A      
-%7D%0A
       return
@@ -1150,16 +1150,22 @@
 ));%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 788