Unique Word Abbreviation - Problem
Imagine you're building a smart text compression system that creates abbreviations for words. Your task is to implement a ValidWordAbbr class that can determine if a word's abbreviation is unique within a given dictionary.
๐ How Abbreviations Work:
- Regular words:
dogโd1g(first letter + count of middle letters + last letter) - Long words:
internationalizationโi18n(18 letters between 'i' and 'n') - Short words:
itโit(words with โค2 characters stay as-is)
๐ฏ Your Mission:
Build a system that returns true if a word's abbreviation is unique in the dictionary, meaning:
- No collision: No other word in the dictionary has the same abbreviation, OR
- Same word: Only the exact same word shares this abbreviation
Example: If dictionary contains ["deer", "door"], both have abbreviation d2r. So isUnique("dear") returns false (collision), but isUnique("deer") returns true (same word).
Input & Output
example_1.py โ Basic Dictionary
$
Input:
ValidWordAbbr(["deer", "door", "cake", "card"])
validator.isUnique("dear")
validator.isUnique("cart")
validator.isUnique("cane")
validator.isUnique("make")
โบ
Output:
false
false
true
true
๐ก Note:
"dear" โ "d2r" conflicts with "deer" and "door". "cart" โ "c2t" conflicts with "card" โ "c2d" (different abbreviations but both start with c and end with different letters). "cane" โ "c2e" conflicts with "cake", but wait - "cake" โ "c2e" too! So "cane" is not unique. "make" โ "m2e" has no conflicts.
example_2.py โ Edge Cases
$
Input:
ValidWordAbbr(["a", "aa"])
validator.isUnique("a")
validator.isUnique("aa")
validator.isUnique("aaa")
โบ
Output:
true
true
true
๐ก Note:
Short words (โค2 chars) keep original form: "a" โ "a", "aa" โ "aa", "aaa" โ "a1a". Each has unique abbreviation.
example_3.py โ Same Word Check
$
Input:
ValidWordAbbr(["deer", "door", "deer"])
validator.isUnique("deer")
validator.isUnique("door")
validator.isUnique("dear")
โบ
Output:
false
false
false
๐ก Note:
Even though "deer" appears twice in dictionary, both "deer" and "door" map to "d2r", so neither is unique. "dear" also maps to "d2r" but isn't in the dictionary.
Visualization
Tap to expand
Understanding the Visualization
1
Build the catalog
Create index cards mapping each abbreviation to all books with that code
2
Handle new requests
When someone asks about a book code, instantly check the card catalog
3
Check for conflicts
If multiple different books share the same code, it's not unique
4
Return result
Only return 'unique' if no other books use this code, or if all books with this code are identical
Key Takeaway
๐ฏ Key Insight: By preprocessing the dictionary into a hash map grouped by abbreviations, we transform an O(n) search problem into an O(1) lookup problem, making the system highly efficient for repeated queries.
Time & Space Complexity
Time Complexity
O(n + q)
O(n) preprocessing + O(1) per query, where n = dictionary size, q = number of queries
โ Linear Growth
Space Complexity
O(n)
Hash map stores all dictionary words grouped by abbreviation
โก Linearithmic Space
Constraints
- 1 โค dictionary.length โค 3 ร 104
- 1 โค dictionary[i].length โค 20
- dictionary[i] consists of lowercase English letters only
- 1 โค word.length โค 20
- word consists of lowercase English letters only
- At most 5000 calls will be made to isUnique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code