package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.List;

/**
 * 93. Restore IP Addresses
 *
 * Given a string containing only digits, restore it by returning all possible valid IP address
 * combinations.
 *
 * For example: Given "25525511135",
 *
 * return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
 */
public class _93 {

  public static class Solution1 {
    public List<String> restoreIpAddresses(String s) {
      List<String> allValidIpAddresses = new ArrayList<>();
      if (s == null || s.length() > 12 || s.length() < 4) {
        return allValidIpAddresses;
      }
      backtracking(s, new ArrayList<>(), allValidIpAddresses, 0);
      return allValidIpAddresses;
    }

    private void backtracking(String s, ArrayList<String> bytes, List<String> result, int pos) {
      if (bytes.size() == 4) {
        if (pos != s.length()) {
          return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 4; i++) {
          stringBuilder.append(bytes.get(i));
          stringBuilder.append(".");
        }
        stringBuilder.setLength(stringBuilder.length() - 1);
        result.add(stringBuilder.toString());
        return;
      }

      for (int i = pos; i < pos + 4 && i < s.length(); i++) {
        String oneByte = s.substring(pos, i + 1);
        if (!isValid(oneByte)) {
          continue;
        }
        bytes.add(oneByte);
        backtracking(s, bytes, result, i + 1);
        bytes.remove(bytes.size() - 1);
      }
    }

    private boolean isValid(String oneByte) {
      if (oneByte.charAt(0) == '0') {
        return oneByte.equals("0");
      }
      int num = Integer.valueOf(oneByte);
      return (num >= 0 && num < 256);
    }
  }
}

@@ -85,275 +85,8 @@
 t;%0A%0A
-/**%0A * 93. Restore IP Addresses%0A *%0A * Given a string containing only digits, restore it by returning all possible valid IP address%0A * combinations.%0A *%0A * For example: Given %2225525511135%22,%0A *%0A * return %5B%22255.255.11.135%22, %22255.255.111.35%22%5D. (Order does not matter)%0A */%0A
 publ
@@ -101,16 +101,18 @@
  _93 %7B%0A%0A
+  
   public
@@ -137,24 +137,28 @@
 tion1 %7B%0A    
+  
+  
 public List%3C
@@ -192,24 +192,30 @@
 String s) %7B%0A
+      
       List%3CS
@@ -264,24 +264,30 @@
 %3C%3E();%0A      
+ 
+     
 if (s == nul
@@ -332,24 +332,32 @@
 ) %7B%0A        
+   
+     
 return allVa
@@ -374,26 +374,38 @@
 sses;%0A      
-%7D%0A
+      %7D%0A      
       backtr
@@ -458,16 +458,22 @@
 es, 0);%0A
+      
       re
@@ -498,27 +498,35 @@
 resses;%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     private 
@@ -612,24 +612,30 @@
 os) %7B%0A      
+   
+   
 if (bytes.si
@@ -651,24 +651,32 @@
 ) %7B%0A        
+      
+  
 if (pos != s
@@ -690,32 +690,42 @@
 ()) %7B%0A          
+      
+    
 return;%0A        
@@ -724,26 +724,39 @@
         
+     
-%7D%0A
+   %7D%0A     
         
 StringBu
@@ -743,24 +743,27 @@
             
+   
 StringBuilde
@@ -797,24 +797,32 @@
 gBuilder();%0A
+        
         for 
@@ -849,32 +849,42 @@
 ++) %7B%0A          
+      
+    
 stringBuilder.ap
@@ -899,24 +899,34 @@
 es.get(i));%0A
+          
           st
@@ -950,34 +950,50 @@
 d(%22.%22);%0A        
+   
-%7D%0A
+     %7D%0A        
         stringBu
@@ -1045,16 +1045,24 @@
         
+   
+     
 result.a
@@ -1087,24 +1087,32 @@
 oString());%0A
+        
         retu
@@ -1117,27 +1117,39 @@
 turn;%0A      
+    
+  
 %7D%0A%0A
+      
       for (i
@@ -1190,32 +1190,40 @@
 ength(); i++) %7B%0A
+        
         String o
@@ -1260,24 +1260,32 @@
 1);%0A        
+ 
+       
 if (!isValid
@@ -1293,24 +1293,34 @@
 oneByte)) %7B%0A
+          
           co
@@ -1327,34 +1327,50 @@
 ntinue;%0A        
-%7D%0A
+        %7D%0A        
         bytes.ad
@@ -1381,32 +1381,40 @@
 eByte);%0A        
+ 
+       
 backtracking(s, 
@@ -1440,24 +1440,32 @@
 1);%0A        
+  
+      
 bytes.remove
@@ -1494,17 +1494,31 @@
     
+  
-%7D%0A
+    %7D%0A  
     
+  
 %7D%0A%0A
+    
     
@@ -1561,24 +1561,30 @@
 te) %7B%0A      
+     
+ 
 if (oneByte.
@@ -1599,24 +1599,32 @@
 ) == '0') %7B%0A
+        
         retu
@@ -1653,24 +1653,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A  
       
+    
 int num 
@@ -1711,24 +1711,30 @@
 yte);%0A      
+    
+  
 return (num 
@@ -1757,16 +1757,22 @@
 6);%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 93