Unique Word Abbreviation

Problem: Unique Word Abbreviation

We can use a hash table to maintain most the abbreviations. There are some details need to be covered. Same words in dictionary are allowed, while different words with the same abbreviation need to be dealt with.

Code in Python:

class ValidWordAbbr(object):
    def __init__(self, dictionary):
        """
        initialize your data structure here.
        :type dictionary: List[str]
        """
        self.dict = {}
        for word in dictionary:
            abbr = word if len(word) <= 2 else word[0]+str(len(word)-2)+word[-1]
            if abbr not in self.dict: self.dict[abbr] = word
            elif self.dict[abbr] != word: self.dict[abbr] = False


    def isUnique(self, word):
        """
        check if a word is unique.
        :type word: str
        :rtype: bool
        """
        abbr = word if len(word) <= 2 else word[0]+str(len(word)-2)+word[-1]
        if abbr in self.dict: return self.dict[abbr] == word
        return True



# Your ValidWordAbbr object will be instantiated and called as such:
# vwa = ValidWordAbbr(dictionary)
# vwa.isUnique("word")
# vwa.isUnique("anotherWord")

results matching ""

    No results matching ""