[
groupby
string
simulation
]
Leetcode 0038. Count and Say
Problem statement
https://leetcode.com/problems/count-and-say/
Solution
Just do what it says, efficient algorithm is to use groupby function from python.
Complexity
Potentially sequence can extend upto O(2^n)
elements, so we have O(2^n)
time and space complexity.
Code
class Solution:
def countAndSay(self, n):
state = "1"
for _ in range(n-1):
state = "".join([str(len(list(j))) + str(i) for i, j in groupby(state)])
return state