intgetN(char c){//对字符标记 if (c == 'Q') return0; if (c == 'W') return1; if (c == 'E') return2; if (c == 'R') return3; }
intgetRes(string s){ int count[4] = { 0, 0, 0, 0 }; int ans = s.size(); int avg = s.size() / 4; for (int i = 0; i != s.size(); i++) count[getN(s[i])]++;//记录4个字符的个数
int i = 0; for (int j = 0; j != s.size(); j++) {//活动窗口i, j左右指针 count[getN(s[j])]--; while (i < s.size() && count[0] <= avg && count[1] <= avg && count[2] <= avg && count[3] <= avg) {//找到了可以替换的字符串 ans = min(ans, j - i + 1);//计算字符串长度 count[getN(s[i++])]++; } }