Skip to content

Commit 95aa94f

Browse files
authored
Update 459-repeated-substring-pattern.js
1 parent fc5a4bc commit 95aa94f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

459-repeated-substring-pattern.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const repeatedSubstringPattern = function(s) {
6+
7+
const len = s.length
8+
const table = build(s)
9+
return table[len] && (table[len] % (len - table[len]) === 0)
10+
11+
function build(str) {
12+
const n = str.length
13+
const table = Array(n + 1).fill(0)
14+
let i = 1, j = 0
15+
table[0] = -1
16+
while(i < n) {
17+
if(str[i] === str[j]) {
18+
i++
19+
j++
20+
table[i] = j
21+
} else {
22+
if(j > 0) j = table[j]
23+
else i++
24+
}
25+
}
26+
27+
return table
28+
}
29+
};
30+
31+
// another
32+
133
/**
234
* @param {string} s
335
* @return {boolean}

0 commit comments

Comments
 (0)