编程语言
首页 > 编程语言> > python写算法题:leetcode: 97. Interleaving String

python写算法题:leetcode: 97. Interleaving String

作者:互联网


class Solution(object):
    def isInterleave(self, s1, s2, s3):
        """
        :type s1: str
        :type s2: str
        :type s3: str
        :rtype: bool
        """
        if len(s1)+len(s2)!=len(s3):
            return False
        path=[]
        p0=-1
        p1=-1
        i=0
        checked=set()

        while i<len(s3):
            matched=0
            if (p0,p1,i) not in checked:
                if p0+1<len(s1) and s1[p0+1]==s3[i]:
                    checked.add((p0,p1,i))
                    p0+=1
                    matched=1
                if p1+1<len(s2) and s2[p1+1]==s3[i]:
                    if matched==1:
                        path.append((i+1,p0,p1+1))
                    else:
                        matched=1
                        p1+=1
            if matched==1:
                i+=1
            else:
                if len(path)>0:
                    i=path[-1][0]
                    p0=path[-1][1]-1
                    p1=path[-1][2]
                    del path[-1]
                else:
                    return False
        return True

 

标签:return,String,python,s2,Interleaving,s3,path,type,s1
来源: https://blog.csdn.net/lzf_china/article/details/98941016