Python using generator

  • 0
    import itertools
    class Solution(object):
        def countAndSay(self, n):
            return ''.join(self.say(n))
        def say(self, n):
            if n == 1:
                yield '1'
            for key, group in itertools.groupby(self.say(n - 1)):
                for ch in str(sum(1 for _ in group)):
                    yield ch
                yield key

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.