-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalancedString.cpp
More file actions
23 lines (22 loc) · 936 Bytes
/
balancedString.cpp
File metadata and controls
23 lines (22 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int balancedString(string s) {
int n = s.length(), m = n / 4, cnt['X']{};
for (char c : s) ++cnt[c];
if (cnt['Q'] == m && cnt['W'] == m && cnt['E'] == m && cnt['R'] == m)
return 0;
int ans = n, left = 0;
for (int right = 0; right < n; right++) { // 枚举子串右端点
--cnt[s[right]];
while (cnt['Q'] <= m && cnt['W'] <= m && cnt['E'] <= m && cnt['R'] <= m) {
ans = min(ans, right - left + 1);
++cnt[s[left++]]; // 缩小子串
}
}
return ans;
}
};
// 作者:灵茶山艾府
// 链接:https://leetcode.cn/problems/replace-the-substring-for-balanced-string/solutions/2108358/tong-xiang-shuang-zhi-zhen-hua-dong-chua-z7tu/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。