leetcode——686. 重复叠加字符串匹配

还好吧,用了半小时多一点,但是好在效果挺好:

class Solution:
    def repeatedStringMatch(self, A: str, B: str) -> int:
        if len(B)<=len(A):
            if B in A:
                return 1
            else:
                if B in A*2:
                    return 2
                else:
                    return -1
        if len(B)>len(A):
            if A not in B:
                if B in A*2:
                    return 2
                else:
                    return -1
            else:
                c=B.count(A)
                if A*c==B:
                    return c
                d=B.index(A)
                e=len(B)-len(A)*c-d
                if B[:d]!=A[len(A)-d:] or B[len(A)*c+d: ]!=A[:e]:
                    return -1
                else:
                    if d==0 or e==0:
                        return c+1
                    else:
                        return c+2
执行用时 :44 ms, 在所有 python3 提交中击败了98.61%的用户
内存消耗 :13.7 MB, 在所有 python3 提交中击败了5.81%的用户
 
 
                                                              ——2019.10.18
posted @ 2019-10-18 15:33  欣姐姐  阅读(278)  评论(0编辑  收藏  举报