leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2272.cpp (771B)


0 class Solution { 1 public: 2 int largestVariance(const string &s) { 3 int count[27] = {0}, res = 0; 4 for (char c : s) 5 count[c & 0xF]++; 6 7 for (char ma = 'a'; ma <= 'z'; ma++) { 8 for (char mi = 'a'; mi <= 'z'; mi++) { 9 if (ma == mi || !count[ma & 0xF] || !count[mi & 0xF]) continue; 10 int mac = 0, mic = 0, rst = count[mi & 0xF]; 11 for (char c : s) { 12 if (c == ma) 13 mac++; 14 else if (c == mi) 15 mic++, rst--; 16 17 if (mic > 0) res = max(res, mac - mic); 18 if (mac < mic && rst > 0) mac = mic = 0; 19 } 20 } 21 } 22 return res; 23 } 24 };