# 3090. Maximum Length Substring With Two Occurrences

```cpp
class Solution {
public:
    int maximumLengthSubstring(string s) {
        int m1[26]{};
        int count = 0;
        
        // sliding window part
        for(int l = 0, r = 0; r < s.length(); ++r)
        {   
            int a = s[r] - 'a';
            ++m1[a];
            while(m1[a]  > 2)
                --m1[s[l++] - 'a'];
            count = max(count, r - l + 1);
        }

        return count;
    }
};
```

**Explanation**

This is a classic sliding window problem, we first define a an empty array of size 26, we will be counting the ascii, we define a count variable to keep track of length of the answer, what we do is we use a left pointer and a right pointer, we keep on increasing the right pointer, if the count of character at s\[r\] remains below or equal to 2 as per question description, once the count of character s\[r\] goes above 2, we increase the left pointer and decrease the count, until the count again becomes equal or lower to 2, in that way, we adjust the window and shrink it, until we get the max count by using r - l + 1.
