Skip to content

Commit 7a79ac8

Browse files
authored
Update 459-repeated-substring-pattern.js
1 parent 358d339 commit 7a79ac8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

459-repeated-substring-pattern.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ const repeatedSubstringPattern = function (s) {
5454
return prefix
5555
}
5656
}
57+
58+
// another
59+
60+
/**
61+
* @param {string} s
62+
* @return {boolean}
63+
*/
64+
const repeatedSubstringPattern = function(s) {
65+
let i = 1, j = 0, n = s.length;
66+
const dp = Array(n + 1).fill(0);
67+
while( i < s.length ){
68+
if( s[i] === s[j] ) dp[++i] = ++j;
69+
else if( j === 0 ) i++;
70+
else j = dp[j];
71+
}
72+
return dp[n] && (dp[n] % (n - dp[n]) === 0);
73+
};

0 commit comments

Comments
 (0)