Unique Word Abbreviation Problem
Description
LeetCode Problem 288.
The abbreviation of a word is a concatenation of its first letter, the number of characters between the first and last letter, and its last letter. If a word has only two characters, then it is an abbreviation of itself.
For example:
dog --> d1gbecause there is one letter between the first letter'd'and the last letter'g'.internationalization --> i18nbecause there are 18 letters between the first letter'i'and the last letter'n'.it --> itbecause any word with only two characters is an abbreviation of itself.
Implement the ValidWordAbbr class:
ValidWordAbbr(String[] dictionary)Initializes the object with adictionaryof words.boolean isUnique(string word)Returnstrueif either of the following conditions are met (otherwise returnsfalse):- There is no word in
dictionarywhose abbreviation is equal toword's abbreviation. - For any word in
dictionarywhose abbreviation is equal toword's abbreviation, that word andwordare the same.
- There is no word in
Example 1:
Input
["ValidWordAbbr", "isUnique", "isUnique", "isUnique", "isUnique", "isUnique"]
[[["deer", "door", "cake", "card"]], ["dear"], ["cart"], ["cane"], ["make"], ["cake"]]
Output
[null, false, true, false, true, true]
Explanation
ValidWordAbbr validWordAbbr = new ValidWordAbbr(["deer", "door", "cake", "card"]);
validWordAbbr.isUnique("dear"); // return false, dictionary word "deer" and word "dear" have the same abbreviation "d2r" but are not the same.
validWordAbbr.isUnique("cart"); // return true, no words in the dictionary have the abbreviation "c2t".
validWordAbbr.isUnique("cane"); // return false, dictionary word "cake" and word "cane" have the same abbreviation "c2e" but are not the same.
validWordAbbr.isUnique("make"); // return true, no words in the dictionary have the abbreviation "m2e".
validWordAbbr.isUnique("cake"); // return true, because "cake" is already in the dictionary and no other word in the dictionary has "c2e" abbreviation.
Constraints:
1 <= dictionary.length <= 3 * 10^41 <= dictionary[i].length <= 20dictionary[i]consists of lowercase English letters.1 <= word.length <= 20wordconsists of lowercase English letters.- At most
5000calls will be made toisUnique.
Solution
Approach: Hash Map from Abbreviation to Its Word
A word is unique when every dictionary word with the same abbreviation is that same word (or there are none). So for each abbreviation, all we need to know is: which single word has it, or whether two different words share it.
In the constructor, map each abbreviation to its owner:
- The first word with an abbreviation becomes its owner.
- If a different word has the same abbreviation later, mark it as shared with
""(no real word is empty). - The same word appearing twice in the dictionary is fine: it is still one owner.
Then isUnique(word) computes abbr(word) and returns true if the abbreviation is not in the map, or if its owner is exactly word. A shared abbreviation ("") never matches.
In the example, "deer" and "door" both give d2r, so d2r is shared and isUnique("dear") is false. c2e belongs only to "cake", so isUnique("cake") is true but isUnique("cane") is false.
The abbreviation is the first letter, the count of letters in between, and the last letter. A word of length 2 or less is its own abbreviation.
Complexity: O(total length of the dictionary) time to build the map, O(L) per isUnique call for a word of length L. O(number of dictionary words) space.
class ValidWordAbbr:
def __init__(self, dictionary: List[str]):
# abbreviation -> the one dictionary word with it, or "" when two different words share it
self.owner = {}
for word in dictionary:
a = self.abbr(word)
if a in self.owner and self.owner[a] != word:
self.owner[a] = ""
else:
self.owner[a] = word
def isUnique(self, word: str) -> bool:
a = self.abbr(word)
return a not in self.owner or self.owner[a] == word
@staticmethod
def abbr(word: str) -> str:
n = len(word)
return word if n <= 2 else word[0] + str(n - 2) + word[-1]