package com.fishercoder.solutions;

/**
 * 87. Scramble String
 *
 * Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

 Below is one possible representation of s1 = "great":

     great
    /    \
   gr    eat
  / \    /  \
 g   r  e   at
            / \
           a   t
 To scramble the string, we may choose any non-leaf node and swap its two children.

 For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

     rgeat
    /    \
   rg    eat
  / \    /  \
 r   g  e   at
            / \
           a   t
 We say that "rgeat" is a scrambled string of "great".

 Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

      rgtae
     /    \
   rg    tae
  / \    /  \
 r   g  ta  e
       / \
      t   a
 We say that "rgtae" is a scrambled string of "great".

 Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
 */
public class _87 {

    public static class Solution1 {
        /** credit: https://discuss.leetcode.com/topic/19158/accepted-java-solution */
        public boolean isScramble(String s1, String s2) {
          if (s1.equals(s2)) {
            return true;
          }
          if (s1.length() != s2.length()) {
            return false;
          }

          int[] letters = new int[26];
          for (int i = 0; i < s1.length(); i++) {
            letters[s1.charAt(i) - 'a']++;
            letters[s2.charAt(i) - 'a']--;
          }

          for (int i : letters) {
            if (i != 0) {
              return false;
            }
          }

          for (int i = 1; i < s1.length(); i++) {
            if (isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(
                s1.substring(i), s2.substring(i))) {
              return true;
            }
            if (isScramble(s1.substring(0, i), s2.substring(s2.length() - i)) && isScramble(
                s1.substring(i), s2.substring(0, s2.length() - i))) {
              return true;
            }
          }
          return false;
        }
    }
}

@@ -33,1014 +33,8 @@
 s;%0A%0A
-/**%0A * 87. Scramble String%0A *%0A * Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.%0A%0A Below is one possible representation of s1 = %22great%22:%0A%0A     great%0A    /    %5C%0A   gr    eat%0A  / %5C    /  %5C%0A g   r  e   at%0A            / %5C%0A           a   t%0A To scramble the string, we may choose any non-leaf node and swap its two children.%0A%0A For example, if we choose the node %22gr%22 and swap its two children, it produces a scrambled string %22rgeat%22.%0A%0A     rgeat%0A    /    %5C%0A   rg    eat%0A  / %5C    /  %5C%0A r   g  e   at%0A            / %5C%0A           a   t%0A We say that %22rgeat%22 is a scrambled string of %22great%22.%0A%0A Similarly, if we continue to swap the children of nodes %22eat%22 and %22at%22, it produces a scrambled string %22rgtae%22.%0A%0A      rgtae%0A     /    %5C%0A   rg    tae%0A  / %5C    /  %5C%0A r   g  ta  e%0A       / %5C%0A      t   a%0A We say that %22rgtae%22 is a scrambled string of %22great%22.%0A%0A Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.%0A */%0A
 publ
@@ -96,16 +96,27 @@
      /**
+%0A         *
  credit:
@@ -179,16 +179,25 @@
 solution
+%0A        
  */%0A    
@@ -256,24 +256,26 @@
 %7B%0A          
+  
 if (s1.equal
@@ -287,32 +287,36 @@
 ) %7B%0A            
+ 
+   
 return true;%0A   
@@ -314,34 +314,38 @@
 true;%0A          
-%7D%0A
+  %7D%0A  
           if (s1
@@ -376,32 +376,36 @@
 ) %7B%0A            
+  
+  
 return false;%0A  
@@ -404,32 +404,34 @@
 alse;%0A          
+  
 %7D%0A%0A          int
@@ -427,16 +427,18 @@
         
+ 
+ 
 int%5B%5D le
@@ -458,16 +458,18 @@
 nt%5B26%5D;%0A
+  
         
@@ -514,32 +514,36 @@
 ) %7B%0A            
+ 
+   
 letters%5Bs1.charA
@@ -557,16 +557,20 @@
 'a'%5D++;%0A
+    
         
@@ -604,16 +604,18 @@
 'a'%5D--;%0A
+  
         
@@ -621,32 +621,34 @@
    %7D%0A%0A          
+  
 for (int i : let
@@ -663,24 +663,28 @@
             
+    
 if (i != 0) 
@@ -691,32 +691,38 @@
 %7B%0A              
+  
+    
 return false;%0A  
@@ -723,32 +723,36 @@
 se;%0A            
+    
 %7D%0A          %7D%0A%0A 
@@ -739,35 +739,39 @@
     %7D%0A          
+ 
+ 
 %7D%0A%0A
+  
           for (i
@@ -796,32 +796,36 @@
 ength(); i++) %7B%0A
+    
             if (
@@ -898,32 +898,40 @@
                 
+  
+      
 s1.substring(i),
@@ -957,32 +957,38 @@
 %7B%0A              
+      
 return true;%0A   
@@ -988,32 +988,36 @@
 ue;%0A            
+    
 %7D%0A            if
@@ -1006,32 +1006,36 @@
   %7D%0A            
+ 
+   
 if (isScramble(s
@@ -1107,32 +1107,40 @@
                 
+      
+  
 s1.substring(i),
@@ -1183,32 +1183,38 @@
 %7B%0A              
+      
 return true;%0A   
@@ -1202,32 +1202,50 @@
    return true;%0A
+                %7D%0A
             %7D%0A  
@@ -1244,26 +1244,16 @@
     %7D%0A  
-        %7D%0A
         

refactor 87