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
Word Abbreviation - Trie-based Approach INPUT String Array (9 words) "like" "god" "internal" "me" "internet" "interval" "intension" "face" "intrusion" Build Trie by Last Char root e l t like,face internal internet interval... ALGORITHM STEPS 1 Group by Last Char Build tries for words ending with same char 2 Initial Abbreviation first + count + last internet --> i6t 3 Resolve Conflicts Extend prefix until unique in trie 4 Length Check Keep original if abbr not shorter Conflict Resolution Example intension, intrusion Both start as i7n i7n == i7n (conflict!) Extend prefix in trie: inte4n, intr4n (OK) FINAL RESULT Abbreviated Words "like" --> "l2e" "god" (kept) "internal" "me" (kept) internet-->i6t "interval" intension-->inte4n "face" --> "f2e" intrusion-->intr4n Output Array: ["l2e", "god", "internal", "me", "i6t", "interval", "inte4n", "f2e", "intr4n"] 9 words - OK Saved 15 characters total Key Insight: The Trie groups words by their last character, then stores reversed prefixes. When finding the minimum unique prefix, traverse the trie until reaching a node with count=1, meaning no other word shares that prefix. This ensures O(n*k) complexity where k is average word length, avoiding pairwise comparisons. TutorialsPoint - Word Abbreviation | Trie-based Approach
Asked in
Google 15 Facebook 12 Amazon 8
28.5K Views
Medium Frequency
~35 min Avg. Time
842 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