0038. Count and Say
Medium | String | 36 ms (96.09%), 14.3 MB (74.99%)
Last updated
Was this helpful?
Medium | String | 36 ms (96.09%), 14.3 MB (74.99%)
Last updated
Was this helpful?
Source: GitHub:
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
countAndSay(1) = "1"
countAndSay(n)
is the way you would "say" the digit string from countAndSay(n-1)
, which is then converted into a different digit string.
To determine how you "say" a digit string, split it into the minimal number of groups so that each group is a contiguous section all of the same character. Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying.
Referring to each char in the previous sequence and record the previous char to check whether we need to do accumulation or restart the counter with the new previous char.