Clean python code

  • 0
    class ValidWordAbbr(object):
        def __init__(self, dictionary):
            self.record = {}
            for word in dictionary:
                abb = self.getAbb(word)
                if word == abb:
                [self.record.update({w: self.record.get(w, 0) + 1})
                 for w in (word, abb)]
        def getAbb(self, word):
            return word if len(word) < 3 else '{}{}{}'.format(
                word[0], len(word) - 2, word[-1])
        def isUnique(self, word):
            abb = self.getAbb(word)
            return not (
                word in self.record and self.record[abb] > 1 or
                word not in self.record and abb in self.record)

Log in to reply

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