您的位置:首页 > 其它

[LeetCode]87 Scramble String

2015-01-06 10:59 330 查看
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