2018-11-15 leetcode Count and Say Count and Saygroupby Version - 40ms 1234567891011import itertoolsclass Solution: def countAndSay(self, n): """ :type n: int :rtype: str """ ret = '1' for _ in range(n - 1): ret = ''.join(str(len(list(group))) + digit for digit, group in itertools.groupby(ret)) return ret Prev Version - 52ms 1234567891011121314151617181920class Solution: def countAndSay(self, n): """ :type n: int :rtype: str """ ret = '1' for _ in range(n - 1): prev = ret ret = '' j = 0 while j < len(prev): cur = prev[j] cnt = 1 j += 1 while j < len(prev) and prev[j] == cur: cnt += 1 j += 1 ret += str(cnt) + str(cur) return ret test code 12In [472]: s = Solution(); t = s.countAndSay(8); print(t)1113213211 leet code Newer Longest Increasing Path in a Matrix Older Number of Islands