text: class Solution {
  private :
      void storeOutput(vector<char> &buf, char ch, int cnt) {
          buf.push_back(ch);
          if (cnt > 1) {
              stack<char> sp;
              for (; cnt > 0; cnt /= 10) {
                  char digit = '0' + (cnt % 10);
                  sp.push(digit);
              }
              while (!sp.empty()) {
                  buf.push_back(sp.top());
                  sp.pop();
              }
          }        
      }
  
  public:
      int compress(vector<char>& chars) {
          vector<char> buf;
          int cnt = 0;
          char ch = chars[0];
  
          for_each(chars.begin(), chars.end(), [this, &buf, &cnt, &ch](const auto val) {
              if (ch == val) {
                  ++cnt;
                  return;
              }
              storeOutput(buf, ch, cnt);
              ch = val;
              cnt = 1;
          });
          storeOutput(buf, ch, cnt);
          chars.clear();
          chars.assign(buf.begin(), buf.end());
  
          return buf.size();
      }
  };
  