Posted on

Description

Submission

class Solution {
public:
    string countAndSay(int n) {
        if(n == 1) return "1";
        string s = "1";
        for(int i = 0; i < n - 1; ++i) {
            int count = 0;
            char prev = s[0];
            stringstream ss;
            for(char c : s) {
                if(c == prev) count++;
                else {
                    ss << count << prev;
                    count = 1;
                }
                prev = c;
            }
            ss << count << prev;
            s = ss.str();
        }
        return s;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *