07(March) Longest repeating and non-overlapping substring
07. Longest repeating and non-overlapping substring
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class Solution {
public:
string longestSubstring(string S, int N) {
// code here
int maxLen = 0;
string ans = "-1";
int i = 0, j = 0;
while (i < N && j < N) {
string subString = S.substr(i, j - i + 1);
if (S.find(subString, j + 1) != string::npos) {
int len = subString.length();
if (len > maxLen) {
maxLen = len;
ans = subString;
}
} else {
i++;
}
j++;
}
return ans;
}
};Contribution and Support
📍Visitor Count
Previous06(March) Search Pattern (Rabin-Karp Algorithm)Next08(March) Check if frequencies can be equal
Last updated