Python using regular dict, 209 ms, beats 78%


  • 0
    R
    class ValidWordAbbr(object):
        def __init__(self, dictionary):
            self.dc = {}
            for i in dictionary:
                if len(i) > 2:
                    s = i[0] + str(len(i)-2) + i[-1]
                    if s in self.dc:
                        self.dc[s].append(i)
                    else:
                        self.dc[s] = [i]
                else:
                    self.dc[i] = [i]
    
        def isUnique(self, word):
            val = word
            if len(word) > 2:
                val = val[0] + str(len(val)-2) + val[-1]
            return val not in self.dc or (len(self.dc[val]) == 1 and self.dc[val][0] == word)
    
    

Log in to reply
 

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