---
# System prepended metadata

title: 38. Count and Say
tags: [C++, LeetCode, Medium]

---

# 38. Count and Say
###### tags: `LeetCode` `C++` `Medium`

## Notes
```
= example 1 =

1 1 2 1 1 -> 2 1 1 2 2 1

每次一串相同的數字結束後
就需要把收集到的資訊 push 進 result
需要注意的是
除了一串相同的數字結束之外
還有當字串結束時
也要把資訊 push 進 result

step 1 : 
相同數字結束 push
1 1 2 1 1 -> 2 1
---

step 2 :
相同數字結束 push
1 1 2 1 1 -> 2 1 1 2
-----

step 3 :
字串結束 push
1 1 2 1 1 -> 2 1 1 2 2 1
---------
```

## Code
```c++
#include <vector>
#include <string>
#include "cout.h"

string countAndSay(int n) ;
vector<int> countAndSayVector(vector<int> v);

int main()
{
    cout << countAndSay(1) << endl;
    return 0;
}

string countAndSay(int n) 
{
    vector<int> v = {1};
    int len = 0;
    string s;

    if(n == 1)
    {
        return string("1");
    }

    for(int i = 0; i < n - 1; i++)
    {
        v = countAndSayVector(v);
        coutVector(v);
    }

    len = v.size();
    for(int i = 0; i < len; i++)
    {
        s.append(to_string(v[i]));
    }
    return s;
}

vector<int> countAndSayVector(vector<int> v)
{
    vector<int> result;
    int len = v.size();
    int count = 1;
    int lastNum = v[0];
    int currentNum = 0;

    if(len == 1)
    {
        result.push_back(1);
        result.push_back(v[0]);
        return result;
    }

    for(int i = 1; i < len; i++)
    {
        currentNum = v[i];
        if(lastNum == currentNum)
        {
            count++;
        }
        else
        {
            result.push_back(count);
            result.push_back(lastNum);
            lastNum = currentNum;
            count = 1;
        }
        
        if(i == len - 1)
        {
            result.push_back(count);
            result.push_back(lastNum);
        }
    }

    return result;
}
```