Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:
Input:**** "abab"
Output: True
Explanation: It’s the substring "ab" twice.

Example 2:
Input: "aba"
Output: False

Example 3:
Input: "abcabcabcabc"
Output: True
Explanation: It’s the substring "abc" four times. (And the substring "abcabc" twice.)

Solution in python:

class Solution:
    def repeatedSubstringPattern(self, s: str) -> bool:
        length = len(s)
        alist = []
        i = 1
        while i < length//2+1:
            if length % i == 0:
                alist.append(i)
            i += 1
        for value in alist:
            k = length // value
            for i in range(k):
                print(s[0:value],s[i*value:(i+1)*value])
                if s[0:value] != s[i*value:(i+1)*value]:
                    break
                if i == k-1:
                    return True
        return False
最后修改日期: 2021年1月29日

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。