Skip to content

459 Repeated Substring Pattern

459. Repeated Substring Pattern

题目: https://leetcode.com/problems/Repeated-Substring-Pattern/

难度:

Easy

思路

  • 如果存在这样的子串,那么子串的第一个字符和最后一个字符肯定跟父字符串s的相同。
  • 因此构建一个新字符串s*2(两个父字符串相加),去掉首尾字符
  • 如果此时能在其中找到s,说明存在这样的子串
class Solution(object):
    def repeatedSubstringPattern(self, s):
        """
        :type s: str
        :rtype: bool
        """
        return (s*2)[1:-1].find(s) != -1

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组