#include
#include<unordered_map>
#include
using namespace std;
int maxt(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int LongestSubstringTwoDistinctCharacters(string str)
{
int res = 0;
unordered_map<char, int> mapt;
int left = 0;
for (int i = 0; i < str.size(); i++)
{
mapt[str[i]]++;
while(mapt.size() > 2)
{
if (–mapt[str[left]] == 0) mapt.erase(str[left]);
/主要是为了连续字符的情况aaa/
left++;
}
res = maxt(res, i - left + 1);
}
return res;
}
int main()
{
cout << LongestSubstringTwoDistinctCharacters(“eceba”) << endl;
system(“pause”);
return 0;
}