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:

  1. No collision: No other word in the dictionary has the same abbreviation, OR
  2. 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
Library Catalog System AnalogyCard Catalogd2r โ†’ {deer, door}c2e โ†’ {cake}c2d โ†’ {card}New Book Request"dear" โ†’ d2rCheck catalog...Conflict Found!d2r is used by:โ€ข deer โ‰  dearโ€ข door โ‰  dearNOT UNIQUE๐Ÿ’ก Key Insight: Preprocessing creates instant O(1) lookupsinstead of scanning the entire library every time
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

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map stores all dictionary words grouped by abbreviation

n
2n
โšก 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
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen