simple idea using stack


  • 0
    Y

    code is kinda sloppy... but the idea is very simple. Code can be optimized further

    class Solution(object):
        def generateAbbreviations(self, word):
            limit = 0xffffffff >> (32 - len(word))
            res = []
            for i in range(limit+1):
                stack = []
                for j in range(len(word)):
                    num = 0
                    if i & (1<<j):
                        if stack and type(stack[-1]) == int:
                            stack[-1] += 1
                        else:
                            stack.append(1)
                    else:
                        stack.append(word[j])
                stack = map(str, stack)
                res.append(''.join(stack))
            return res
    

Log in to reply
 

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