aboutsummaryrefslogtreecommitdiff
path: root/count_and_say.cpp
blob: a1086b4ecf5ff91520a2ec15ca45b99ebf0936b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "leetcode.h"

class Solution {
public:
  string countAndSay(int n) {
    if (n == 0)
      return "";
    string ans = "1";
    while (--n) {
      string curr = "";
      for (int i = 0; i < ans.size(); i++) {
        int count = 1;
        while ((i + 1 < ans.size()) && (ans[i] == ans[i + 1]))
          count++, i++;
        curr += to_string(count) + ans[i];
      }
      ans = curr;
    }
    return ans;
  }
};

int main() {
  Solution obj;
  cout << obj.countAndSay(4);
}