Clean BFS solution


  • 0
    class Solution(object):
        def generateAbbreviations(self, word):
            if not word:
                return ['']
    
            queue = collections.deque([[word[0]], [1]])
            for char in word[1:]:
                for _ in xrange(len(queue)):
                    node = queue.popleft()
                    queue.append(node + [char])
                    if isinstance(node[-1], int):
                        node[-1] += 1
                        queue.append(node)
                    else:
                        queue.append(node + [1])
            return map(lambda item: ''.join(map(str, item)), queue)

Log in to reply
 

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