Skip to main content

Word Abbreviation - Solution & Explanation

HardPremiumFree on FleetCodeArrayStringGreedyTrie13 min readAsked at: Amazon, Google, Applied Intuition +1
Practice this problem

Problem Statement

Given an array of distinct strings words, return the minimal possible abbreviations for every word.

The following are the rules for a string abbreviation:

  1. The initial abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.
  2. 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.
      • For example, say you start with the words ["abcdef","abndef"] both initially abbreviated as "a4f". Then, a sequence of operations would be ["a4f","a4f"] -> ["ab3f","ab3f"] -> ["abc2f","abn2f"].
    • This operation is repeated until every abbreviation is unique.
  3. At the end, if an abbreviation did not make a word shorter, then keep it as the original word.

 

Example 1:

Input: words = ["like","god","internal","me","internet","interval","intension","face","intrusion"]
Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]

Example 2:

Input: words = ["aa","aaa"]
Output: ["aa","aaa"]

 

Constraints:

  • 1 <= words.length <= 400
  • 2 <= words[i].length <= 400
  • words[i] consists of lowercase English letters.
  • All the strings of words are unique.

Approach Overview

Problem Overview: Given a list of distinct words, generate the shortest possible abbreviation for each word such that no two abbreviations collide. An abbreviation keeps the first letter, last letter, and replaces the middle characters with their count. If multiple words produce the same abbreviation, you must increase the prefix length until every abbreviation becomes unique.

Approach 1: Pairwise Prefix Expansion (Brute Force) (Time: O(n2 * L), Space: O(n))

Start by generating the default abbreviation for every word using prefix length 1. Then compare every pair of words. When two abbreviations collide, increase the prefix length for both words and recompute their abbreviations. This process continues until all conflicts disappear. The key operation is repeatedly checking pairs and rebuilding abbreviations, which becomes expensive for large inputs. This approach is straightforward and demonstrates the rule behind abbreviation conflicts but scales poorly because every conflict may trigger multiple recomputations.

Approach 2: Sorting + Longest Common Prefix (Time: O(n log n * L), Space: O(n))

Sort words by their potential abbreviation group: same first letter, last letter, and length. Within each group, only nearby words in sorted order can collide. Compute the longest common prefix between adjacent words to determine the minimum prefix required to differentiate them. This reduces unnecessary comparisons across unrelated words. Sorting clusters similar words together, and prefix comparison determines how many characters must remain uncompressed. This approach improves performance significantly and uses standard sorting and string operations.

Approach 3: Grouped Trie (Optimal) (Time: O(n * L), Space: O(n * L))

Group words by (length, first character, last character) because only these can produce identical abbreviations. For each group, build a Trie where each node tracks how many words pass through it. While inserting characters, the first node with count 1 identifies the unique prefix length for that word. Once the unique prefix is known, build the abbreviation using that prefix plus the compressed middle count. The Trie efficiently detects the earliest divergence between words, eliminating repeated comparisons. Each character is processed once during insertion and lookup, producing near-linear complexity relative to total characters.

Recommended for interviews: The grouped Trie approach is the expected solution. It demonstrates control over array grouping, Trie design, and greedy prefix selection. Mentioning the brute-force idea shows you understand the conflict rule, but implementing the Trie-based grouping proves you can scale the solution efficiently.

Solution

We notice that if two words have the same abbreviation, their first and last letters must be the same, and their lengths must be the same. Therefore, we can group all words by length and last letter, and use a trie to store the information of each group of words.

The structure of each node in the trie is as follows:

  • children: An array of length 26, representing all child nodes of this node.
  • cnt: The number of words passing through this node.

For each word, we insert it into the trie and record the cnt value of each node.

When querying, we start from the root node. For the current letter, if the cnt value of its corresponding child node is 1, then we have found the unique abbreviation, and we return the length of the current prefix. Otherwise, we continue to traverse downwards. After the traversal, if we have not found a unique abbreviation, then we return the length of the original word. After getting the prefix lengths of all words, we check whether the abbreviation of the word is shorter than the original word. If it is, then we add it to the answer, otherwise we add the original word to the answer.

The time complexity is O(L), and the space complexity is O(L). Here, L is the sum of the lengths of all words.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Pairwise Prefix Expansion (Brute Force)O(n^2 * L)O(n)Small input sizes or when demonstrating the basic conflict resolution logic
Sorting + Longest Common PrefixO(n log n * L)O(n)When using standard string operations without building additional data structures
Grouped Trie (Optimal)O(n * L)O(n * L)Best for large datasets and interview settings where efficient prefix detection is required

Video Solution

LeetCode 527. Word AbbreviationHappy Coding3,498 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Word Abbreviation easy or hard?
Word Abbreviation is considered a hard problem because it combines greedy abbreviation rules with prefix uniqueness detection. Efficient solutions require grouping logic and a Trie to avoid quadratic comparisons.
Word Abbreviation Python/Java solution
Implement the grouped Trie approach. First group words by length and boundary letters, then build a Trie for each group while counting prefix frequencies. After determining the unique prefix length for each word, generate the abbreviation using prefix + skipped length + last character.
How to solve Word Abbreviation in O(n)?
Group words with the same length, first letter, and last letter. Build a Trie for each group and track how many words pass through each node. The first node with count 1 gives the shortest unique prefix, allowing the abbreviation to be generated in linear time relative to total characters.
What is the best approach for Word Abbreviation?
The grouped Trie approach is the most efficient and commonly expected interview solution. Words are grouped by length and boundary characters, then inserted into a Trie to find the earliest unique prefix. This avoids repeated comparisons and runs in O(n * L) time where L is the average word length.
Is Word Abbreviation asked at Google/Amazon/Meta?
Word Abbreviation is a classic string and Trie design problem that has appeared in interviews at large tech companies including Google and Meta. It tests conflict resolution, prefix uniqueness, and efficient string processing.
What data structure is used in Word Abbreviation?
The optimal solution uses a Trie (prefix tree). The Trie helps track how many words share the same prefix, making it easy to determine the shortest unique prefix required to avoid abbreviation collisions.
What is the time complexity of Word Abbreviation?
The optimal grouped Trie solution runs in O(n * L) time because each character of every word is inserted and traversed once. Space complexity is also O(n * L) due to the Trie structure storing characters for all grouped words.

Ready to solve this problem?

Practice Word Abbreviation with our built-in code editor and test cases.

Practice on FleetCode