Skip to content

Instantly share code, notes, and snippets.

@nags0x
Created June 11, 2025 14:32
Show Gist options
  • Select an option

  • Save nags0x/f5733b08d8890acb26a251c9ff091d1a to your computer and use it in GitHub Desktop.

Select an option

Save nags0x/f5733b08d8890acb26a251c9ff091d1a to your computer and use it in GitHub Desktop.
#3445 -- need to fix tle
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