题目描述:
有两个字符串A和B,求A重复几次时,B是A的一部分。
结题思路:
- 当B not in A时,就让A+=A(注意这里用A的浅拷贝,只要加上A最初的值就行)count+=1
- 如果A的长度或者B的长度大于10000,还没找到B,说明B是无法成为A中的子集了,返回-1
代码:
class Solution:
def repeatedStringMatch(self, A, B):
"""
:type A: str
:type B: str
:rtype: int
"""
count = 1
cp = copy.copy(A)
while B not in A:
A += cp
count += 1
if len(A) >10000 or len(B) > 10000:
return -1
return count