Word Abbreviation - Problem
Given an array of distinct strings words, return the minimal possible abbreviations for every word.
The following are the rules for a string abbreviation:
- The initial abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.
- If more than one word shares the same abbreviation, then perform the following operation: Increase the prefix (characters in the first part) of each of their abbreviations by 1.
- This operation is repeated until every abbreviation is unique.
- At the end, if an abbreviation did not make a word shorter, then keep it as the original word.
Example: For words ["abcdef","abndef"], both initially abbreviated as "a4f". The sequence would be ["a4f","a4f"] → ["ab3f","ab3f"] → ["abc2f","abn2f"].
Input & Output
Example 1 — Basic Conflict Resolution
$
Input:
words = ["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"]
›
Output:
["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]
💡 Note:
Words with conflicts need longer prefixes: 'internal', 'internet', 'interval', 'intension', 'intrusion' all start with 'i', so they need different prefix lengths to become unique.
Example 2 — Simple Case
$
Input:
words = ["aa", "aaa"]
›
Output:
["aa","aaa"]
💡 Note:
Both words are too short to abbreviate meaningfully, so they remain unchanged.
Example 3 — No Conflicts
$
Input:
words = ["word", "test", "sample"]
›
Output:
["w2d","t2t","s4e"]
💡 Note:
All words have different first characters, so simple abbreviation with 1-character prefix works for all.
Constraints
- 1 ≤ words.length ≤ 400
- 2 ≤ words[i].length ≤ 400
- words[i] consists of lowercase English letters
- All the strings of words are unique
Visualization
Tap to expand
Understanding the Visualization
1
Input Words
Array of distinct strings
2
Find Conflicts
Identify words with same initial abbreviation
3
Resolve Conflicts
Extend prefixes until all abbreviations are unique
Key Takeaway
🎯 Key Insight: Extend prefixes incrementally only for conflicting words to achieve minimal unique abbreviations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code