Created
June 11, 2025 14:32
-
-
Save nags0x/f5733b08d8890acb26a251c9ff091d1a to your computer and use it in GitHub Desktop.
#3445 -- need to fix tle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int maxDifference(string s, int k) { | |
| int maxDiff = INT_MIN; | |
| int n = s.size(); | |
| for (int win = k; win <= n; ++win) { | |
| unordered_map<char, int> freq; | |
| for (int i = 0; i < win; ++i) { | |
| freq[s[i]]++; | |
| } | |
| for (auto& a : freq) { | |
| if (a.second % 2 == 1) { | |
| for (auto& b : freq) { | |
| if (b.second % 2 == 0) { | |
| maxDiff = max(maxDiff, a.second - b.second); | |
| } | |
| } | |
| } | |
| } | |
| for (int i = win; i < n; ++i) { | |
| freq[s[i - win]]--; | |
| if (freq[s[i - win]] == 0) { | |
| freq.erase(s[i - win]); | |
| } | |
| freq[s[i]]++; | |
| for (auto& a : freq) { | |
| if (a.second % 2 == 1) { | |
| for (auto& b : freq) { | |
| if (b.second % 2 == 0) { | |
| maxDiff = max(maxDiff, a.second - b.second); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return maxDiff; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment