Python beats 90.7%

  • 0
    class Solution(object):
        def generateAbbreviations(self, word):
            :type word: str
            :rtype: List[str]
            res = []
            self.helper("", res, word, 0)
            return res
        def helper(self, path, res, word, num):
            if len(word) == 0:
                if not num:
                    res.append(path + str(num))
            #if the first letter remain
            path1 = path + word[0] if num == 0 else path + str(num) + word[0]
            self.helper(path1, res, word[1:], 0)
            #if the first letter not remain
            self.helper(path, res, word[1:], num + 1)

Log in to reply

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