Skip to content

Commit 162c1fc

Browse files
authored
Create 1062-longest-repeating-substring.js
1 parent f103c1d commit 162c1fc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

1062-longest-repeating-substring.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const longestRepeatingSubstring = function(s) {
6+
const n = s.length;
7+
// dp[i][j] means # of repeated chars for substrings ending at i and j
8+
const dp = Array.from({ length: n + 1 }, () => Array(n + 1).fill(0));
9+
let res = 0;
10+
for (let i = 1; i <= n; i++) {
11+
for (let j = i + 1; j <= n; j++) {
12+
if (s.charAt(i - 1) === s.charAt(j - 1)) {
13+
dp[i][j] = dp[i - 1][j - 1] + 1;
14+
res = Math.max(res, dp[i][j]);
15+
}
16+
}
17+
}
18+
return res;
19+
};

0 commit comments

Comments
 (0)