Problem Statement
Given a string s, return the maximum length of a substring such that it contains at most two occurrences of each character.
Examples
Example 1:
Input: s = “bcbbbcba”
Output: 4
Explanation:
The following substring has length of 4 and contains at most two occurrences of each character: .bcbbbcba
Example 2:
Input: s = “aaaa”
Output: 2
Explanation:
The following substring has a length of 2 and contains at most two occurrences of each character aa.aa
Different Approaches
1 Sliding Window
Algorithm:
- Initialize variables left and right to represent the left and right boundaries of the window, and a hashmap count to store the count of characters in the window.
- Iterate through the string s:
- Increment the count of the character at index right.
- If the count of characters in the window exceeds 2, shrink the window from the left until the count is 2.
- Update the maximum length of the window.
- Return the maximum length of the window.
Code Implementation C++:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int maxLengthSubstringWithTwoOccurrences(string s) {
int n = s.size(), ans = 0, begin = 0, end = 0;
map<char, int> mp;
while (end < n) {
mp[s[end]]++;
// Shrink the window until there are at most two occurrences of each character
while (mp[s[end]] == 3)
mp[s[begin++]]--;
// Update the maximum length of the substring
ans = max(ans, end - begin + 1);
end++;
}
return ans;
}
int main() {
string s1 = "ccaabbb";
string s2 = "aaa";
cout << "Maximum length of substring with two occurrences (s1): " << maxLengthSubstringWithTwoOccurrences(s1) << endl;
cout << "Maximum length of substring with two occurrences (s2): " << maxLengthSubstringWithTwoOccurrences(s2) << endl;
return 0;
}
Explanation:
1 Initialization:
int n = s.size(), ans = 0, begin = 0, end = 0;
map<char, int> mp;
n: stores the size of the input strings.ans: Initialized to 0 to store the maximum length of the substring with most two occurrences of each character.beginandend: Pointers to represent the window boundaries.mp: A map to store the count of occurrences of each character in the current window.
2 Sliding Window Approach:
while (end < n) {
mp[s[end]]++;
// Shrink the window until there are at most two occurrences of each character
while (mp[s[end]] == 3)
mp[s[begin++]]--;
// Update the maximum length of the substring
ans = max(ans, end - begin + 1);
end++;
}
- We iterate through the string using the end pointer.
- At each step, we update the count of the character at index end in the
mpmap. - If the count of the character exceeds two (indicating more than two occurrences), we shrink the window from the begin pointer until there are at most two occurrences of each character.
- We update the maximum length of the substring
answith the maximum length found so far. - Finally, we move the
endpointer to expand the window.
3 Returning the Result:
return ans;- Once we have iterated through the entire string and updated the maximum length of the substring
ans, we return it.

Join the discussion
Sign in with your account to post comments, reply to others, and participate in the conversation.
Login to Comment