[LeetCode] Longest Repeating Substring

1062. Longest Repeating Substring

Given a string s, return the length of the longest repeating substrings. If no repeating substring exists, return 0.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int longestRepeatingSubstring(string s) {
int n = s.length(), res = 0;
vector<vector<int>> dp(n + 1, vector<int>(n+1));
for(int i = 0; i < n; i++)
for(int j = 0; j < i; j++)
if(s[i] == s[j])
res = max(res, dp[i+1][j+1] = 1 + dp[i][j]);
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/28/PS/LeetCode/longest-repeating-substring/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.