title: Why is the memory limit exceeded?
body: <p>I was doing the &quot;Sort Characters By Frequency&quot; problem on the <a href="https://leetcode.com/" rel="nofollow noreferrer">LeetCode</a> <a href="https://en.wikipedia.org/wiki/Competitive_programming#Overview" rel="nofollow noreferrer">online judge</a>.</p>
  <p>Why does this happen?</p>
  <h3>Code</h3>
  <pre><code>class Solution {
  
      public:
          struct Node {
              int freq;
              char ch;
          };
  
          static bool lambda(struct Node a, struct Node b) {
              return a.freq &gt; b.freq;
          }
  
          string frequencySort(string s) {
              if(s.size() == 0)
                  return &quot;&quot;;
  
              string res = &quot;&quot;;
              vector&lt;Node&gt; nums(256);
  
              for(char c : s) {
                  nums[(int)c].ch = c;
                  nums[(int)c].freq++;
              }
  
              std::sort(nums.begin(), nums.end(), Solution::lambda);
  
              char c;
              for(int i=0; nums[i].freq &gt; 0; i++) {
                  c = nums[i].ch;
                  while(nums[i].freq--) {
                      res = res + c; // If I replace this line with res += c, it gets accepted!
                  }
              }
  
              return res;
          }
  };
  </code></pre>