# LeetCode 38. Count and Say

## Description

The count-and-say sequence is the sequence of integers with the first five terms as following:

```1.     1
2.     11
3.     21
4.     1211
5.     111221
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
```

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

```Example 1:

Input: 1
Output: "1"
Example 2:

Input: 4
Output: "1211"
```

## 描述

```1.     1
2.     11
3.     21
4.     1211
5.     111221
1 被读作  "one 1"  ("一个一") , 即 11。
11 被读作 "two 1s" ("两个一"）, 即 21。
21 被读作 "one 2",  "one 1" （"一个二" ,  "一个一") , 即 1211。
```

```输入: 1

```
```输入: 4

```

### 思路

• itertools 的 groupby 模块能够对可迭代对象进行统计。
• 利用 groupby 统计结果，并形成新的字符串；然后再对新的字符串进行统计；一次循环 n – 1 次即可。
```# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-08-18 09:49:01

from itertools import groupby

class Solution:
def countAndSay(self, n: int) -> str:
result = '1'
for _ in range(n - 1):
result = ''.join([str(len(list(g))) + key for key, g in groupby(result)])

return result
```