The question/task: You are given a list of n different words. Your task is to find a set of their shortened variants. The shortened variants of all words from the list should be different.
My reasoning: Finding Unique Shortened Variants for Words
Problem Analysis
We need to find shortened variants (typically the shortest unique prefixes) for a list of words such that all shortened variants are distinct. Solution Approach
Key Insight
The shortest unique prefix for a word is determined by its closest matches in alphabetical order. By sorting the words, words with common prefixes become adjacent, making it efficient to find the minimum prefix length needed. Algorithm
- For each word, compare with neighbors (previous and next in sorted order)
- Find minimum distinguishing prefix length - the maximum of distances needed to distinguish from both neighbors
- Extract and return the prefixes
Step-by-Step Solution
Algorithm: Shortest Unique Prefix
Input: List of n distinct words
Output: List of n unique shortened variants (prefixes)
1. If has previous neighbor:
- Find first position where they differ
- Update min_prefix_len = max(min_prefix_len, diff_pos + 1)
c. If has next neighbor:
- Find first position where they differ
- Update min_prefix_len = max(min_prefix_len, diff_pos + 1)
d. Return result list
Worked Example
Input: ["apple", "apricot", "banana", "berry", "application"]
Step 1: Sort alphabetically
| Sorted Order | Word | Original Index |
0 | apple | 0 |
| 1 | application | 4 |
| 2 | apricot | 1 |
| 3 | banana | 2 |
| 4 | berry | 3 |
Step 2: Find unique prefix for each
| Word | Prev Word | Next Word | Diff with Prev | Diff with Next | Min Prefix Len | Shortened |
apple | - | application | - | pos 4 | 5 | "apple" |
| application | apple | apricot | pos 4 | pos 2 | 5 | "appli" |
| apricot | application | banana | pos 2 | pos 0 | 3 | "apr" |
| banana | apricot | berry | pos 0 | pos 1 | 2 | "ba" |
| berry | banana | - | pos 1 | - | 2 | "be" |
Output: ["apple", "apr", "ba", "be", "appli"]
Verification
Let's verify all shortened variants are unique:
- "apple" ✓
- "appli" ✓
- "apr" ✓
- "ba" ✓
- "be" ✓
All 5 shortened variants are distinct ✓
Python Implementation
def find_unique_prefixes(words):
n = len(words)
if n == 0:
return []
Create indexed pairs and sort
indexed = [(i, word) for i, word in enumerate(words)]
indexed.sort(key=lambda x: x[1])
result = [None] * n
for i in range(n):
orig_idx, word = indexed[i]
min_len = 1
Compare with previous
if i > 0:
prev = indexed[i-1][1]
for j in range(min(len(word), len(prev))):
if word[j] != prev[j]:
min_len = max(min_len, j + 1)
break
else:
min_len = max(min_len, min(len(word), len(prev)) + 1)
Compare with next
if i < n - 1:
nxt = indexed[i+1][1]
for j in range(min(len(word), len(nxt))):
if word[j] != nxt[j]:
min_len = max(min_len, j + 1)
break
else:
min_len = max(min_len, min(len(word), len(nxt)) + 1)
result[orig_idx] = word[:min_len]
return result
Complexity Analysis
| Metric | Complexity |
Time | O(n log n × m) - sorting plus comparisons |
| Space | O(n) - for storing results |
Where n = number of words, m = average word length
Finding Unique Shortened Variants for Words
Problem Analysis
We need to find shortened variants (typically the shortest unique prefixes) for a list of words such that all shortened variants are distinct.
Solution Approach
Key Insight
The shortest unique prefix for a word is determined by its closest matches in alphabetical order. By sorting the words, words with common prefixes become adjacent, making it efficient to find the minimum prefix length needed.
Algorithm
- Sort words alphabetically (keeping track of original indices)
- For each word, compare with neighbors (previous and next in sorted order)
- Find minimum distinguishing prefix length - the maximum of distances needed to distinguish from both neighbors
- Extract and return the prefixes
Step-by-Step Solution
Algorithm: Shortest Unique Prefix
Input: List of n distinct words
Output: List of n unique shortened variants (prefixes)
1. Create indexed pairs: [(original_index, word), ...]
2. Sort pairs by word alphabetically
3. For each word at sorted position i:
a. Initialize min_prefix_len = 1
b. If has previous neighbor:
- Find first position where they differ
- Update min_prefix_len = max(min_prefix_len, diff_pos + 1)
c. If has next neighbor:
- Find first position where they differ
- Update min_prefix_len = max(min_prefix_len, diff_pos + 1)
d. Store word[:min_prefix_len] at original index
4. Return result list
Worked Example
Input: ["apple", "apricot", "banana", "berry", "application"]
Step 1: Sort alphabetically
| Sorted Order | Word | Original Index |
|---|
| 0 | apple | 0 |
| 1 | application | 4 |
| 2 | apricot | 1 |
| 3 | banana | 2 |
| 4 | berry | 3 |
Step 2: Find unique prefix for each
| Word | Prev Word | Next Word | Diff with Prev | Diff with Next | Min Prefix Len | Shortened |
|---|
| apple | - | application | - | pos 4 | 5 | "apple" |
| application | apple | apricot | pos 4 | pos 2 | 5 | "appli" |
| apricot | application | banana | pos 2 | pos 0 | 3 | "apr" |
| banana | apricot | berry | pos 0 | pos 1 | 2 | "ba" |
| berry | banana | - | pos 1 | - | 2 | "be" |
Output: ["apple", "apr", "ba", "be", "appli"]
Verification
Let's verify all shortened variants are unique:
- "apple" ✓
- "appli" ✓
- "apr" ✓
- "ba" ✓
- "be" ✓
All 5 shortened variants are distinct ✓
Python Implementation
def find_unique_prefixes(words):
n = len(words)
if n == 0:
return []
# Create indexed pairs and sort
indexed = [(i, word) for i, word in enumerate(words)]
indexed.sort(key=lambda x: x[1])
result = [None] * n
for i in range(n):
orig_idx, word = indexed[i]
min_len = 1
# Compare with previous
if i > 0:
prev = indexed[i-1][1]
for j in range(min(len(word), len(prev))):
if word[j] != prev[j]:
min_len = max(min_len, j + 1)
break
else:
min_len = max(min_len, min(len(word), len(prev)) + 1)
# Compare with next
if i < n - 1:
nxt = indexed[i+1][1]
for j in range(min(len(word), len(nxt))):
if word[j] != nxt[j]:
min_len = max(min_len, j + 1)
break
else:
min_len = max(min_len, min(len(word), len(nxt)) + 1)
result[orig_idx] = word[:min_len]
return result
Complexity Analysis
| Metric | Complexity |
|---|
| Time | O(n log n × m) - sorting plus comparisons |
| Space | O(n) - for storing results |
Where n = number of words, m = average word length