28. 实现 strStr()
题目链接/文章讲解/视频讲解:代码随想录
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
return haystack.find(needle)
n = len(haystack)
m = len(needle)
for i in range(n):
if haystack[i:i+m] == needle:
return i
return -1
459.重复的子字符串
题目链接/文章讲解/视频讲解:代码随想录
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
n = len(s)
tmp = ''
for i in range(n-1):
tmp += s[i]
cnt = n % len(tmp)
temp = n // len(tmp)
if cnt == 0 and tmp * temp == s:
return True
return False