Minimum Unique Word Abbreviation - Problem
Find the shortest unique abbreviation for a target word!
You're given a
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.
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
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
⚠ Quadratic Growth
Space Complexity
O(2^n)
Store all possible abbreviations before sorting
⚠ 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code