Problem Description
Given a dictionary of words, implement a class ValidWordAbbr that can tell if a word’s abbreviation is unique. The abbreviation of a word is defined as the concatenation of its first letter, the count of characters between the first and last letters, and its last letter (e.g., "internationalization" becomes "i18n"). A word with only two characters is its own abbreviation. The function isUnique should return true if either no word in the dictionary shares the same abbreviation as the input word, or if all words that share the same abbreviation are exactly the same as the input word.
Key Insights
- Compute the abbreviation for each word using its first letter, count of characters in-between, and last letter.
- Use a hash table (dictionary/map) to store each abbreviation as a key and the corresponding word or set of words from the dictionary as the value.
- When checking if an input word’s abbreviation is unique, verify if the abbreviation is absent in the map or if it exists but only maps to the same word.
- Care must be taken when handling duplicate words in the dictionary.
Space and Time Complexity
Time Complexity:
- Constructor: O(N * L) where N is the number of words in the dictionary and L is the average word length.
- isUnique: O(L), since computing the abbreviation for the input word takes linear time. Space Complexity:
- O(N * L) in the worst case due to storing abbreviations and corresponding words.
Solution
We use a hash table to map each computed abbreviation to the word from the dictionary if it is unique, or to a special marker (or set) if there are multiple distinct words that share that abbreviation. In the constructor, for each word in the dictionary, we compute its abbreviation. If the abbreviation is not present, we add it with the word as its value. If it is already present and the stored word is different from the current one, we mark this abbreviation as conflicting by storing a value that indicates multiple words (e.g., a special marker like an empty string or a set of words). In the isUnique method, we compute the abbreviation of the input word and check:
- If the abbreviation does not exist in our map, the input word is unique.
- If the abbreviation exists and it maps exactly to the input word (and no conflict exists), it is unique.
- Otherwise, it is not unique. A small nuance is ensuring that duplicate words in the dictionary don’t cause a false conflict.