Leetcode Problem 288. Unique Word Abbreviation

288. Unique Word Abbreviation

Leetcode Solutions

Hash Table with Preprocessing

  1. Define the constructor of the ValidWordAbbr class to accept a list of words as the dictionary.
  2. Initialize a hash table to store the mapping from abbreviations to sets of words.
  3. Iterate over each word in the dictionary and compute its abbreviation.
  4. Add the word to the set corresponding to its abbreviation in the hash table.
  5. Define the isUnique method to check if a given word's abbreviation is unique.
  6. Compute the abbreviation for the given word.
  7. Check if the abbreviation exists in the hash table.
  8. If it does not exist, return true as the word is unique.
  9. If it exists, check if the set contains only the given word or if the set is empty. If so, return true.
  10. Otherwise, return false.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...