-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2272.cpp
More file actions
25 lines (23 loc) · 771 Bytes
/
Copy path2272.cpp
File metadata and controls
25 lines (23 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
int largestVariance(const string &s) {
int count[27] = {0}, res = 0;
for (char c : s)
count[c & 0xF]++;
for (char ma = 'a'; ma <= 'z'; ma++) {
for (char mi = 'a'; mi <= 'z'; mi++) {
if (ma == mi || !count[ma & 0xF] || !count[mi & 0xF]) continue;
int mac = 0, mic = 0, rst = count[mi & 0xF];
for (char c : s) {
if (c == ma)
mac++;
else if (c == mi)
mic++, rst--;
if (mic > 0) res = max(res, mac - mic);
if (mac < mic && rst > 0) mac = mic = 0;
}
}
}
return res;
}
};