class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
int windowStart = 0, maxLength = 0;
unordered_map<char, int> table;
for (int windowEnd = 0; windowEnd < s.length(); windowEnd++) {
table[s[windowEnd]]++;
while (table.size() > 2) {
if (--table[s[windowStart]] == 0) {
table.erase(s[windowStart]);
}
windowStart++;
}
maxLength = max(maxLength, windowEnd-windowStart+1);
}
return maxLength;
}
};