← Back to LeetCode Notes

3090. Maximum Length Substring With Two Occurrences

easy · hash-table · string · sliding-window · solved 2026.08.16 · 10 sec read

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