标签:leetcode
https://oj.leetcode.com/problems/scramble-string/
http://blog.csdn.net/linhuanmars/article/details/24506703
public class Solution { public boolean isScramble(String s1, String s2) { if (s1 == null || s2 == null || s1.length() != s2.length()) return false; if (s1.isEmpty() && s2.isEmpty()) return true; int length = s1.length(); boolean r[][][] = new boolean[length][length][length + 1]; // 1 -> length; for (int i = 0 ; i < length ; i ++) { for (int j = 0 ; j < length ; j ++) { r[i][j][1] = s1.charAt(i) == s2.charAt(j); } } for (int len = 2 ; len <= length ; len ++) { for (int i = 0 ; i < length - len + 1 ; i ++) { for (int j = 0 ; j < length - len + 1 ; j ++) { for (int k = 1 ; k < len ; k ++) { r[i][j][len] |= (r[i][j][k] && r[i + k][j + k][len - k]) || (r[i][j + len - k][k] && r[i + k][j][len - k]); } } } } return r[0][0][length]; } }
标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1599579