Lintcode 1227. Repeated Substring Pattern (Easy) (Python),lintcoderepeated
分享于 点击 42637 次 点评:105
Lintcode 1227. Repeated Substring Pattern (Easy) (Python),lintcoderepeated
Repeated Substring Pattern
Description:
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.)
Code:
class Solution:
"""
@param s: a string
@return: return a boolean
"""
def repeatedSubstringPattern(self, s):
size = len(s)
for x in range(1, int(size / 2 + 1)):
if size % x:
continue
if s[:x] * (int(size / x)) == s:
return True
return False
class Solution:
"""
@param s: a string
@return: return a boolean
"""
def repeatedSubstringPattern(self, s):
tmp = s[1:]+s[:-1]
return (tmp.find(s) != -1)
相关文章
- 暂无相关文章
用户点评