Ideas:
1 recursion:
Simply put, if s1 and s2 are scramble, there must be a l1 length on s1, and s1 is divided into s11 and s12 segments, which also have s21 and s22.
Then s11 and s21 are scramble and s12 and s22 are scramble;
Either s11 and s22 are scramble and s12 and s21 are scramble.
If you want to use OJ, you must sort the strings and then Pruning them.
Http://blog.unieagle.net/2012/10/23/leetcode%E9%A2%98%E7%9B% AE %EF%BC%9Ascramble-string%EF%BC%8C%E4%B8%89%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/
2 DP:
A three-dimensional array boolean result [len] [len] [len] is used. The first dimension is the length of the substring, and the second dimension is the start index of s1, the third dimension is the start index of s2.
Result [k] [I] [j] indicates whether s1 [I... I + k] can be changed by s2 [j... j + k.
Http://www.blogjava.net/sandy/archive/2013/05/22/399605.html
Package Level5; import java. util. arrays;/*** 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 contains scramble the string, we may choose any non-leaf node and swap its two children. for example, if we choose the node "gr" and s Wap its two children, it produces a scrambled string "rgeat ". rgeat/\ rg eat/\ r g e at/\ a tWe 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 aWe 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 S87 {public static void main (String [] args) {String s1 = "abab "; string s2 = "bbaa"; System. out. println (isScramble (s1, s2); System. out. println (isScrambleDP (s1, s2);} public static boolean isScramble (String s1, String s2) {if (s1.length ()! = S2.length () {return false;} if (s1.length () = 1 & s2.length () = 1) {return s1.charAt (0) = s2.charAt (0);} // after sorting, you can use char [] s1ch = s1.toCharArray (); char [] s2ch = s2.toCharArray (); Arrays. sort (s1ch); Arrays. sort (s2ch); if (! New String (s1ch). equals (new String (s2ch) {return false ;}for (int I = 1; I
= 0; I --) {// s1 [I... I + k] for (int j = len-k; j> = 0; j --) {// s2 [j... j + k] boolean canTransform = false; for (int m = 1; m