You're building a smart text compression system that creates minimal unique abbreviations for words. Given an array of distinct strings, your goal is to generate the shortest possible abbreviations for each word while ensuring all abbreviations remain unique.
How abbreviations work:
- Start with:
first_char + count_of_middle_chars + last_char - Example:
"hello"→"h3o"(h + 3 middle chars + o)
Conflict resolution: When multiple words share the same abbreviation, incrementally increase the prefix length until all abbreviations become unique.
Special rule: If the final abbreviation isn't shorter than the original word, keep the original word instead.
Example walkthrough:
For ["abcdef", "abndef"]:
- Initial: both become
"a4f"❌ (conflict!) - Expand prefix: both become
"ab3f"❌ (still conflict!) - Expand again:
"abc2f"and"abn2f"✅ (unique!)
Input & Output
Time & Space Complexity
n words, each processed once through Trie of depth m (average word length)
Trie storage for all word prefixes, in worst case all prefixes are different
Constraints
- 1 ≤ words.length ≤ 1000
- 1 ≤ words[i].length ≤ 20
- words[i] consists of lowercase English letters only
- All strings in words are distinct