当前位置:网站首页>Force buckle 459 Duplicate substring

Force buckle 459 Duplicate substring

2022-07-07 20:06:00 Tomorrowave

459. Repeated substrings

Given a non empty string s , Check whether it can be formed by repeating one of its substrings multiple times .

Example 1:

Input : s = “abab”
Output : true
explain : But by substring “ab” Repeat twice to form .

Ideas :

First, construct a string of arbitrary length to match another string , Traversal extraction string , Meet or not meet the conditions , You can traverse the parent string in the main function

Code section

class Solution:
    def isPrefixMultiple(self,s:str,lengh):
        cnt=len(s)//lengh
        for i in range(cnt):
            for j in range(lengh):
                if s[i*lengh+j]!=s[j]:
                    return False
        return True

    def repeatedSubstringPattern(self, s: str) -> bool:
        nn=len(s)
        for i in range(1,nn):
            if nn%i==0:
                if self.isPrefixMultiple(s,i):
                    return True

        return False
原网站

版权声明
本文为[Tomorrowave]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071753356420.html