【题目】
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
【示例 1】
输入: “abab”
输出: True
解释: 可由子字符串 “ab” 重复两次构成。
【示例 2】
输入: “aba”
输出: False
【示例 3】
输入: “abcabcabcabc”
输出: True
解释: 可由子字符串 “abc” 重复四次构成。 (或者子字符串 “abcabc” 重复两次构成。)
【代码】
【Python】
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
l=len(s)//2
ll=len(s)
for i in range(1,l+1):
temp=s[:i]
if ll%i==0 and temp*(ll//i)==s:
return True
return False
【掐头去尾s+s】
前几题有碰到与这个方法类似的解题思路
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
return s in (s+s)[1:-1]