链接:
https://leetcode.cn/problems/repeated-substring-pattern/description/
思路:
这题其实挺有意思的,我一开始寻思按照字符读到一个dict里统计各个字符的个数,讲道理每个字符的个数是相同的才对。(我承认我傻了,忽略了aab这种情况)
然后我就开始想,如果能行,那原串肯定是子串的n倍。 然后可以这么做:
1. 求出来子串可能的长度列表。
2. 遍历这个列表,查询子串是否满足条件。
代码:
class Solution: def repeatedSubstringPattern(self, s: str) -> bool: mights = self.get_mights(s) for might in mights[::-1]: slice = s[0:might] flag = True for i in range(0, len(s), might): tmp_sclice = s[i:i+might] if slice != tmp_sclice: flag = False break if flag: return flag return False def get_mights(self, s): length = len(s) res = [] for i in range(1, length): if length % i == 0: res.append(i) return res
标签:459,lc,res,self,length,flag,字符串,might,mights From: https://www.cnblogs.com/bjfu-vth/p/17664594.html