leetcode -- Scramble String -- 重点

https://leetcode.com/problems/scramble-string/

思路1 递归

参考:http://www.cnblogs.com/zuoyuan/p/3777383.html
http://blog.csdn.net/fightforyourdream/article/details/17707187

要先排序,否则不能过OJ

class Solution:
    # @return a boolean
    def isScramble(self, s1, s2):
        if len(s1)!=len(s2): return False
        if s1==s2: return True
        l1=list(s1); l2=list(s2)
        l1.sort();l2.sort()
        if l1!=l2: return False
        length=len(s1)
        for i in range(1,length):
            if self.isScramble(s1[:i],s2[:i]) and self.isScramble(s1[i:],s2[i:]): return True
            if self.isScramble(s1[:i],s2[length-i:]) and self.isScramble(s1[i:],s2[:length-i]): 
            #第一部分长度为i,第二部分为length - i. i 最多到length - 1. 因为不能让length - i为0
            return True
        return False

思路2 三维DP

http://blog.csdn.net/fightforyourdream/article/details/17707187
没仔细看,复习的时候再看

你可能感兴趣的:(LeetCode)