Python


  • 0
    B
    class ValidWordAbbr(object):
    
        def __init__(self, dictionary):
            """
            :type dictionary: List[str]
            """
            #as long as no OTHER word has the same abbreviation as the current word return True
            #also have to check if the abbreviated word in dictionary is indeed the current word.
            self.hashmap = {}
            for v in dictionary:
                if len(v) < 3:
                    self.hashmap[v] = v
                    continue
                s = v[0] + str(len(v)-2) + v[len(v)-1]
                if s not in self.hashmap:
                    self.hashmap[s] = v
                elif self.hashmap[s] == v: continue
                else: self.hashmap[s] = None #two different words have the same
                                           #abbreviation, which means, 
                                           #the condition that no OTHER word has the same 
                                           #abbreviation does not hold true for this abbreviation
    
        def isUnique(self, word):
            """
            :type word: str
            :rtype: bool
            """
            if len(word) < 3 : return True
            s = word[0] + str(len(word)-2) + word[len(word)-1]
            if s in self.hashmap and self.hashmap[s] != word: return False
            return True
    
    # Your ValidWordAbbr object will be instantiated and called as such:
    # obj = ValidWordAbbr(dictionary)
    # param_1 = obj.isUnique(word)
    

Log in to reply
 

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