File tree 5 files changed +73
-0
lines changed
algorithms/RepeatedSubstringPattern
5 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -179,6 +179,7 @@ All solutions will be accepted!
179
179
| 189| [ Rotate Array] ( https://leetcode-cn.com/problems/rotate-array/description/ ) | [ java/py/js] ( ./algorithms/RotateArray ) | Easy|
180
180
| 687| [ Longest Univalue Path] ( https://leetcode-cn.com/problems/longest-univalue-path/description/ ) | [ java/py/js] ( ./algorithms/LongestUnivaluePath ) | Easy|
181
181
| 28| [ Implement Strstr] ( https://leetcode-cn.com/problems/implement-strstr/description/ ) | [ java/py/js] ( ./algorithms/ImplementStrstr ) | Easy|
182
+ | 459| [ Repeated Substring Pattern] ( https://leetcode-cn.com/problems/repeated-substring-pattern/description/ ) | [ java/py/js] ( ./algorithms/RepeatedSubstringPattern ) | Easy|
182
183
183
184
# Database
184
185
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Repeated Substring Pattern
2
+ We can use KMP to solve this problem
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean repeatedSubstringPattern (String s ) {
3
+ int length = s .length (),
4
+ j = -1 ;
5
+ int [] next = new int [length ];
6
+ next [0 ] = -1 ;
7
+
8
+ for (int i = 1 ; i < length ; i ++) {
9
+ while (j >= 0 && s .charAt (i ) != s .charAt (j + 1 )) {
10
+ j = next [j ];
11
+ }
12
+ if (s .charAt (i ) == s .charAt (j + 1 )) {
13
+ j ++;
14
+ }
15
+ next [i ] = j ;
16
+ }
17
+
18
+ int subLength = length - 1 - next [length - 1 ];
19
+ return subLength != length && length % subLength == 0 ;
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+ var repeatedSubstringPattern = function ( s ) {
6
+ let i = j = 0 ,
7
+ next = [ ]
8
+
9
+ while ( i < s . length ) {
10
+ if ( i === 0 || ( s [ i ] !== s [ j ] && j === 0 ) ) {
11
+ next . push ( 0 )
12
+ } else if ( s [ i ] === s [ j ] ) {
13
+ next . push ( j + 1 )
14
+ j ++
15
+ } else if ( s [ i ] !== s [ j ] ) {
16
+ j = next [ j - 1 ]
17
+ continue
18
+ }
19
+ i ++
20
+ }
21
+
22
+ let subLength = s . length - next [ s . length - 1 ]
23
+ return subLength !== s . length && s . length % subLength === 0
24
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def repeatedSubstringPattern (self , s ):
3
+ """
4
+ :type s: str
5
+ :rtype: bool
6
+ """
7
+ # method of exhaustion
8
+ for length in range (1 , len (s ) / 2 + 1 ):
9
+ if len (s ) % length != 0 :
10
+ continue
11
+
12
+ start = 0
13
+ temp = None
14
+ is_equal = True
15
+ while start < len (s ):
16
+ p = s [start :start + length ]
17
+ if temp == None :
18
+ temp = p
19
+ elif temp != p :
20
+ is_equal = False
21
+ break
22
+ start += length
23
+ if is_equal :
24
+ return True
25
+ return False
You can’t perform that action at this time.
0 commit comments