Minimum Unique Word Abbreviation - Problem
Find the shortest unique abbreviation for a target word!

You're given a target string and a dictionary of words. Your mission is to create the shortest possible abbreviation of the target that doesn't conflict with any word in the dictionary.

Abbreviation Rules:
• Replace any non-adjacent substrings with their lengths
• Example: "substitution" → "s10n" (replace middle 10 chars)
• Invalid: "s55n" (adjacent replacements)

Abbreviation Length: Count of remaining letters + count of replaced substrings
• "s10n" has length 3 (2 letters + 1 replacement)
• "su3i1u2on" has length 9 (6 letters + 3 replacements)

Your goal: Return the shortest abbreviation of target that uniquely identifies it from all dictionary words.

Input & Output

example_1.py — Basic Case
$ Input: target = "apple", dictionary = ["blade"]
Output: "a4"
💡 Note: "apple" can be abbreviated as "a4" (a + 4 characters). Since "blade" has the same length but different pattern, "a4" uniquely identifies "apple".
example_2.py — Multiple Conflicts
$ Input: target = "apple", dictionary = ["plain", "amber", "blade"]
Output: "1p3"
💡 Note: We need to avoid conflicts with all dictionary words. "1p3" represents (1 char + p + 3 chars) which uniquely identifies "apple" from the given words.
example_3.py — No Dictionary
$ Input: target = "apple", dictionary = []
Output: "5"
💡 Note: When dictionary is empty, the shortest abbreviation is just the length of the target word, since there are no conflicts to avoid.

Visualization

Tap to expand
DMV License Plate AnalogyCreating shortest unique abbreviation like getting shortest available license plateTarget: APPLEAPPLEExisting Plates (Dictionary):BLADEPLAINAMBERTry Abbreviations (Shortest First):5❌ Could match any 5-letter wordA4✅ Only matches A____1P3✅ Unique, but longer than A4Algorithm Steps1. Filter dictionary to same-length words only2. Generate abbreviations by length (shortest first)3. For each abbreviation:• Check if it could match any dictionary word• If unique, return this abbreviation4. Use backtracking for efficiency5. Prune branches that can't be optimal🎯 Key InsightLike DMV plates, we want the shortest possible identifier that's guaranteed unique.Use bitmasks + backtracking to efficiently explore abbreviation space without generating all 2^n possibilities.
Understanding the Visualization
1
Analyze Conflicts
Check which existing plates could conflict with your desired word
2
Try Abbreviations
Start with shortest possible abbreviations and work up
3
Check Uniqueness
For each abbreviation, verify it doesn't match any existing plate pattern
4
Return First Valid
Return the first abbreviation that's unique among all existing plates
Key Takeaway
🎯 Key Insight: Use backtracking with pruning to find the shortest abbreviation that doesn't conflict with any dictionary word, similar to finding the shortest available license plate!

Time & Space Complexity

Time Complexity
⏱️
O(2^n × m × n)

2^n abbreviations to generate, m dictionary words to check against, n characters per check

n
2n
Quadratic Growth
Space Complexity
O(2^n)

Store all possible abbreviations before sorting

n
2n
Quadratic Space

Constraints

  • 1 ≤ target.length ≤ 21
  • 0 ≤ dictionary.length ≤ 1000
  • 1 ≤ dictionary[i].length ≤ 100
  • target and dictionary[i] consist of lowercase English letters
  • Target and dictionary words contain only lowercase letters
Asked in
Google 28 Amazon 22 Meta 15 Microsoft 12
20.9K 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