BackTracking Based Python Solution


  • 0
    U
    class Solution(object):
        def generateAbbreviations(self, word):
            ans = self.Abbreviations(word[::-1])
            return [''.join(abbrevation) for abbrevation in ans]
            
        def Abbreviations(self, word, index=0):
            """
            :type word: str
            :rtype: List[str]
            """
            if index >= len(word):
                return [[]]
            
            ans = []
            abbrevations = self.Abbreviations(word, index + 1)
            for abbrevation in abbrevations:
                abbrevation.append(word[index])
                ans.append(abbrevation[:])
                abbrevation.pop()
                if len(abbrevation) > 0 and abbrevation[-1].isdigit():
                    abbrevation[-1] = str(int(abbrevation[-1]) + 1)
                else:
                    abbrevation.append('1')
                ans.append(abbrevation[:])
                
            return ans
                
                
            
    

Log in to reply
 

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