Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
73 lines (58 loc) · 1.65 KB

File metadata and controls

73 lines (58 loc) · 1.65 KB
Copy raw file
Download raw file
Edit and raw actions

Difficulty: Easy

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"```


#### Solution

Language: **Java**

```java
class Solution {
    public String countAndSay(int n) {
        String tmp = "1";
        if (n == 1) {
            return tmp;
        }
        for (int i = 1; i < n; i++) {
            tmp = say(tmp);
        }
        return tmp;
    }
​
    private String say(String numStr) {
        char tmp = numStr.charAt(0);
        int counter = 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < numStr.length(); i++) {
            if (numStr.charAt(i) == numStr.charAt(i - 1)) {
                counter++;
            } else {
                sb.append(counter).append(tmp);
                tmp = numStr.charAt(i);
                counter = 1;
            }
        }
        sb.append(counter).append(tmp);
        return sb.toString();
    }
}

Morty Proxy This is a proxified and sanitized view of the page, visit original site.