2023年7月12日 星期三

7/12 每日一題(檢查字串是否由子字串組成)

 Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

 

Example 1:

Input: s = "abab"
Output: true
Explanation: It is the substring "ab" twice.

Example 2:

Input: s = "aba"
Output: false

Example 3:

Input: s = "abcabcabcabc"
Output: true
Explanation: It is the substring "abc" four times or the substring "abcabc" twice.

 

Constraints:

  • 1 <= s.length <= 104
  • s consists of lowercase English letters.


class Solution:
    def repeatedSubstringPattern(self, s: str) -> bool:
        if len(s)==1:
            return False
       
        i=0
        while i < len(s)//2:
            p=s[0:i+1]
            n=len(s)//len(p)
            if p*n == s:
                return True
            i+=1
        return False

標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁