Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Count And Say


Solution


    class Solution {
    public:
        string countAndSay(int n) {
            if(n == 1)
                return "1";
            string ans = "1";
            for(int i = 2; i <= n; i++) {
                string temp = "";
                int count  = 1;
                for(int j = 0; j < ans.length() - 1; j++) {
                    if(ans[j] == ans[j + 1]) {
                        count++;
                    }
                    else {
                        temp += to_string(count) + ans[j];
                        count = 1;
                    }
                }
                temp += to_string(count) + ans[ans.length() - 1];
                ans = temp;
            }
            return ans;
        }
    };