| 686 | Repeated String Match | 33.90% | string b要重复多少次才包含string a,循环最多重复b/a+1次(li718) |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 12 21:45:08 2018
@author: vicky
"""
class Solution:
def repeatedStringMatch(self, A, B):
"""
:type A: str
:type B: str
:rtype: int
"""
k=0
C=str()
while k <= len(B)/len(A)+1:#注意要加1,可能刚好是len(B)/len(A)
C=A+C
k=k+1
if B in C:
return k
return -1
A="abcd"
B="cdabcdab"
print(Solution().repeatedStringMatch(A,B))
本文介绍了一种简单的字符串匹配算法实现,该算法通过重复拼接字符串B直至其长度超过字符串A,判断A是否为B的子串。通过这种方法可以解决特定场景下的字符串匹配问题。
422

被折叠的 条评论
为什么被折叠?



