#38

Count and Say

specialist · 750 · lc medium +31 · 62.4% accepted · 5,078 likes · top 64%

play →

Description

The count-and-say sequence starts with "1"; each subsequent term is the run-length encoding of the previous term, describing consecutive runs of identical digits. Return the nth term.

Example 1:

countAndSay(1) = "1"
countAndSay(2) = RLE of "1" = "11"
countAndSay(3) = RLE of "11" = "21"
countAndSay(4) = RLE of "21" = "1211"

Code

1
2
3