public boolean isScramble(String s1, String s2){
    assert(s1 != null && s2 != null);
    
    if (s1.length() != s2.length())
        return false;
    if (s1.equals(s2))
        return true;
    
    char[] s1Array = s1.toCharArray();
    char[] s2Array = s2.toCharArray();
    Arrays.sort(s1Array);
    Arrays.sort(s2Array);
    String s1New = new String(s1Array);
    String s2New = new String(s2Array);
    if (!s1New.equals(s2New))
        return false;
    
    for (int i = 1; i < s1.length(); i++){
        String s1Left = s1.substring(0,i);
        String s1Right = s1.substring(i);
        String s2Left = s2.substring(0,i);
        String s2Right = s2.substring(i);
        if (isScramble(s1Left,s2Left) && isScramble(s1Right,s2Right))
            return true;
        
        s2Left = s2.substring(0,s1.length() - i);
        s2Right = s2.substring(s1.length() - i);
        if (isScramble(s1Left, s2Right) && isScramble(s1Right, s2Left))
            return true;
    }
    return false;
  }