首页 > 其他 > 详细

[LeetCode]87 Scramble String

时间:2015-01-06 12:08:06      阅读:221      评论:0      收藏:0      [点我收藏+]

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]87 Scramble String

原文:http://7371901.blog.51cto.com/7361901/1599579

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!