Pythonic solution

  • 0
    class ValidWordAbbr(object):
        def __init__(self, dictionary):
            from collections import defaultdict
            self.abbrs = defaultdict(list)
            for word in dictionary:
        def isUnique(self, word):
            words = self.abbrs.get(self.getAbbr(word), [])
            return all(w == word for w in words)
        def getAbbr(self, s):
            middle = s[1:-1]
            return ''.join([s[0], str(len(middle)), s[-1]]) if middle else s

Log in to reply

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