-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeatedSubstringPattern.py
More file actions
69 lines (62 loc) · 1.84 KB
/
repeatedSubstringPattern.py
File metadata and controls
69 lines (62 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#Source : https://leetcode.com/problems/repeated-substring-pattern/
#Author : Yuan Wang
#Date : 2018-07-03
'''
**********************************************************************************
*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.)
**********************************************************************************/
'''
#Self solution
def repeatedSubstringPatternA(s):
"""
:type s: str
:rtype: bool
"""
if not s or len(s) == 1:
return False
if s[:len(s)//2]==s[len(s)//2:]:
return True
for i in range(1,len(s)//2+1):
if s[i] == s[0]:
substring=s[:i]
if substring*(len(s)//len(substring))==s:
return True
return False
'''
Pythonic solution, if the string is repeated, then s must in (s+s)[1:-1]
Time complexity:O(n) Space complexity:O(1)
Basic idea:
First char of input string is first char of repeated substring
Last char of input string is last char of repeated substring
Let S1 = S + S (where S in input string)
Remove 1 and last char of S1. Let this be S2
If S exists in S2 then return true else false
Let i be index in S2 where S starts then repeated substring
length i + 1 and repeated substring S[0: i+1]
'''
def repeatedSubstringPatternB(s):
"""
:type s: str
:rtype: bool
"""
return s in (s + s)[1:-1]
s="abcabcabc"
print(repeatedSubstringPatternA(s))