Loading...
Loading...
Loading...
The count-and-say sequence starts with 1. Each next term reads aloud the previous term: count each group of equal digits and say the count then the digit.
111211211111221Read n and print term n.
Example 1
Input
4
Output
1211
Example 2
Input
5
Output
111221
Read 1211 aloud: one 1, one 2, two 1s.
Try the problem first. Open one hint at a time.
Start with the String "1" and build the next term n - 1 times.
To build the next term, walk through the current one and count each run of equal digits, like run-length compression but with the count before the digit.
Use a StringBuilder for each new term: term 30 has more than 4000 digits.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor