0038. Count and Say
Medium | String | 36 ms (96.09%), 14.3 MB (74.99%)
# n = 1
1 #
# n = 2
11 # one 1's
# n = 3
21 # two 1's
# n = 4
1211 # one 2, and one 1.
# n = 5
111221 # one 1, one 2, and two 1's.s = '1' # base case
s = '11' # preChar = 1, do accumulation because s[i] == s[i+1]
s = '21' # preChar = 2, restart counter because s[i] != s[i+1]
s = '1211' # preChar = 1, counter = 1 -> f'{counter}{preChar}'
# preChar = 2, counter = 1
# preChar = 1, counter = 2
s = '111221' # preChar = 1, counter = 3 -> f'{counter}{preChar}'
# preChar = 2, counter = 2
# preChar = 1, counter = 1Last updated