Count and Say
Count and Say is LeetCode 38 (Medium). The sequence starts with "1", and every later term is made by reading the previous term aloud: for each block of the same digit in a row, say how many there are, then the digit. Given n, return the nth term as a string.
n is at most 30, and term 30 has 4,462 characters, so building every term in turn is fast enough.
- 1 <= n <= 30
Intuition
There is no shortcut to term n: each term is defined only by the one before it, so the solution builds terms 2, 3, …, n in order.
One step is run-length encoding. Split the term into runs, blocks of the same digit standing next to each other, and write each run as its length followed by its digit.
Only neighbouring equal digits form a run. In "1211" the 1 at the front and the 1s at the end are separate runs, so it reads as one 1, one 2, two 1s: "111221".
Each term is defined only from the previous one, so simulate it. The inner loop is run-length encoding: find where a run of equal characters ends, write its length, jump past it. String Compression (443) uses the same scan.
Approach
Before reading on: write out terms 1 to 5 by hand. While reading a term, how do you know a run has ended, and what do you write down at that moment?
Start from term 1
Set s = "1" and repeat the describe step n - 1 times: term 1 is given, so only the terms after it are computed. For n = 1 the loop does not run and "1" is returned.
Find where each run ends
Keep i on the first digit of the current run and move j forward while s[j] == s[i]. When j reaches a different digit or the end of the string, the run is s[i:j], and its length is j - i.
Write the count, then the digit
Append str(j - i) + s[i] to a list of pieces, then set i = j so the next run starts right after this one. Every digit is read once per term. Joining the pieces once at the end avoids rebuilding a growing string for every run.
Move to the next term
When i reaches the end of s, "".join(parts) is the next term. Assign it to s and start the next round; after the last round, return s.
Code in Python, C++ and Java
"1". Each round reads the current term aloud to make the next, so 3 rounds reach term 4.j steps past it: the run has 1 digit so far.j reached the end of the term, so the run is over: one 1. Write the count first, then the digit: "11". Then i jumps to 1."11". It replaces s, and the next round reads it from the start.j steps past it: the run has 1 digit so far.s[1] is also 1, the same digit standing next to the run, so the run grows to 2.j reached the end of the term, so the run is over: two 1s. Write the count first, then the digit: "21". Then i jumps to 2."21". It replaces s, and the next round reads it from the start.j steps past it: the run has 1 digit so far.s[1] is 1, a different digit, so the run is over: one 2. Write the count first, then the digit: "12". Then i jumps to 1, the first digit of the next run, so no digit is read twice.j steps past it: the run has 1 digit so far.j reached the end of the term, so the run is over: one 1. Write the count first, then the digit: "11". Then i jumps to 2."1211". It replaces s. That was round 3, the last one."1211". Each round read its term once from left to right, so the total work is the combined length of the terms."1". Each round reads the current term aloud to make the next, so 4 rounds reach term 5.j steps past it: the run has 1 digit so far.j reached the end of the term, so the run is over: one 1. Write the count first, then the digit: "11". Then i jumps to 1."11". It replaces s, and the next round reads it from the start.j steps past it: the run has 1 digit so far.s[1] is also 1, the same digit standing next to the run, so the run grows to 2.j reached the end of the term, so the run is over: two 1s. Write the count first, then the digit: "21". Then i jumps to 2."21". It replaces s, and the next round reads it from the start.j steps past it: the run has 1 digit so far.s[1] is 1, a different digit, so the run is over: one 2. Write the count first, then the digit: "12". Then i jumps to 1, the first digit of the next run, so no digit is read twice.j steps past it: the run has 1 digit so far.j reached the end of the term, so the run is over: one 1. Write the count first, then the digit: "11". Then i jumps to 2."1211". It replaces s, and the next round reads it from the start.j steps past it: the run has 1 digit so far.s[1] is 2, a different digit, so the run is over: one 1. Write the count first, then the digit: "11". Then i jumps to 1, the first digit of the next run, so no digit is read twice.j steps past it: the run has 1 digit so far.s[2] is 1, a different digit, so the run is over: one 2. Write the count first, then the digit: "12". Then i jumps to 2, the first digit of the next run, so no digit is read twice.j steps past it: the run has 1 digit so far.s[3] is also 1, the same digit standing next to the run, so the run grows to 2.j reached the end of the term, so the run is over: two 1s. Write the count first, then the digit: "21". Then i jumps to 4."111221". It replaces s. That was round 4, the last one."111221". Each round read its term once from left to right, so the total work is the combined length of the terms."1". The loop runs n - 1 = 0 times, so nothing is read."1". No rounds were needed.Common pitfalls
Looping n times instead of n − 1
s = "1" for _ in range(n):
s = "1" for _ in range(n - 1):
"1" already is term 1, so only n - 1 describe steps are needed. Looping n times returns term n + 1: "11" for n = 1.
Writing the digit before the count
parts.append(s[i] + str(j - i))
parts.append(str(j - i) + s[i])
The rule is "how many, then which digit". Swapping the order turns "21" into "2111" instead of "1211", and every later term goes wrong.
Moving i by one instead of to the end of the run
parts.append(str(j - i) + s[i]) i += 1
parts.append(str(j - i) + s[i]) i = j
With i += 1 the scan starts again inside the run it just described, so "11" is read as two 1s and then one 1: "2111" instead of "21".
Complexity
Count and Say FAQ
Is count and say the same as the look and say sequence?
Yes. Mathematicians call it the look-and-say sequence, studied by John Conway. He showed that each term is about 1.3036 times as long as the one before (Conway's constant), and that starting from "1" no digit other than 1, 2 and 3 ever appears. So a count is always a single digit.
Can it be solved recursively?
Yes: countAndSay(n) describes the string returned by countAndSay(n - 1), and countAndSay(1) returns "1". It does the same work but keeps n calls on the stack. LeetCode's follow-up asks for the iterative version shown here.
Is there a shorter way to write it in Python?
itertools.groupby splits a string into runs of equal characters, so one round becomes s = "".join(str(len(list(g))) + d for d, g in groupby(s)). It runs in the same time; the two-pointer loop just shows the run detection explicitly.