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 f103c1d commit 162c1fcCopy full SHA for 162c1fc
1062-longest-repeating-substring.js
@@ -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