# 這道題,可以用暴力方法的思想,然后可以知道能夠拼接成字串的子串
# 長度肯定可以被字串整除,但長度一定小于字串長度的二分之一,
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
# 定義一個串列,用來存放能夠被字串長度整除的子串長度,
possible_length = []
# 定義字串的長度,
length = len(s)
# 求出能夠被字串長度整除的子串所有長度,
for i in range(1,length // 2 + 1):
if length % i == 0:
possible_length.append(i)
print(possible_length)
# 用每一個子串進行和后邊子字串進行比較,
for length1 in possible_length:
# 求出當前長度的子字串,
strs = s[:length1]
# 然后進行遍歷比較
for length2 in range(length1,length - length1 + 1,length1):
print(s[length1:length1 + length2])
# 如果不相等,直接跳出回圈,
if s[length2:length1 + length2] != strs:
break
print(length1, length2)
# 最后判斷遍歷的邊界值,
if length2 == length - length1:
return True
return False
A = Solution()
print(A.repeatedSubstringPattern("ababab"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/24494.html
標籤:Python
上一篇:419遞增子序列
