We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fc5a4bc commit 95aa94fCopy full SHA for 95aa94f
459-repeated-substring-pattern.js
@@ -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
33
/**
34
* @param {string} s
35
* @return {boolean}
0 commit comments