3090. Maximum Length Substring With Two Occurrences
View on LeetCode ↗
C++
class Solution {public: int maximumLengthSubstring(string s) { size_t best = 0; for (int i = 0; i < s.size(); i++) { for (int j = 1; j <= s.size() - i; j++) { std::map<char, int> substringing; string test = s.substr(i, j); bool fail = false; for (auto j : test) { substringing[j]++; if ((substringing[j] > 2)) { fail = true; } } // cout << test << " " << fail << endl; if (!fail) { best = max(test.size(), best); } } } return (int)best; }};
very unoptimal O(n^2) will come back to do with sliding window