I have the following codes trying to sort the string based the the character frequency in descending order.
string frequencySort(string s) {
unordered_map<char,int> m{};
for (char c:s) {
if(m.find(c) != m.end()){
m[c]++;
} else {
m[c] = 1;
}
}
sort(s.begin(), s.end(), [&m](char a, char b) { return m[b] < m[a];});
return s;
}
The code works fine, but if I use "<=" in the lambda function:
[&m](char a, char b) { return m[b] <= m[a];}
I would get heap-buffer-overflow error. Does anyone know why is that? Thanks!