Shortest Uncommon Substring in an Array - Problem
Find the Shortest Unique Substring
You're given an array of
Requirements:
• Find the shortest substring of each string that doesn't occur in any other string
• If multiple shortest substrings exist, choose the lexicographically smallest one
• If no unique substring exists, return an empty string for that position
Example: Given
• "ca" from "cab" doesn't appear in other strings
• "a" from "ad" is unique (though "a" exists in "bad", "ad" doesn't contain "ba")
• "b" from "bad" is the shortest unique substring
• "c" is already unique
You're given an array of
n non-empty strings. Your task is to find, for each string, the shortest substring that appears only in that string and nowhere else in the array.Requirements:
• Find the shortest substring of each string that doesn't occur in any other string
• If multiple shortest substrings exist, choose the lexicographically smallest one
• If no unique substring exists, return an empty string for that position
Example: Given
["cab", "ad", "bad", "c"], the answer is ["ca", "a", "b", "c"] because:• "ca" from "cab" doesn't appear in other strings
• "a" from "ad" is unique (though "a" exists in "bad", "ad" doesn't contain "ba")
• "b" from "bad" is the shortest unique substring
• "c" is already unique
Input & Output
example_1.py — Basic Case
$
Input:
["cab", "ad", "bad", "c"]
›
Output:
["ca", "a", "b", "c"]
💡 Note:
For "cab": "ca" is the shortest unique substring (doesn't appear in others). For "ad": "a" is unique to this string's position. For "bad": "b" is the shortest unique. For "c": the entire string "c" is unique.
example_2.py — Lexicographical Selection
$
Input:
["abc", "acd", "bcd"]
›
Output:
["ab", "ac", "bc"]
💡 Note:
Each string has multiple unique substrings of length 2, but we select the lexicographically smallest one. "ab" < "bc", "ac" < "cd", "bc" < "cd".
example_3.py — No Unique Substring
$
Input:
["abc", "abc"]
›
Output:
["", ""]
💡 Note:
Both strings are identical, so no substring can be unique to either string. Both return empty strings.
Visualization
Tap to expand
Understanding the Visualization
1
Catalog all fingerprints
Extract all possible fingerprints (substrings) from every document
2
Count occurrences
Track how many times each fingerprint appears across all documents
3
Find unique markers
For each document, find fingerprints that appear only in that document
4
Select optimal identifier
Choose the shortest, alphabetically first unique fingerprint
Key Takeaway
🎯 Key Insight: The algorithm systematically checks substrings by increasing length and lexicographical order, ensuring we find the optimal (shortest, alphabetically smallest) unique identifier for each string.
Time & Space Complexity
Time Complexity
O(n * m³)
For n strings of average length m, we generate O(m²) substrings per string and check each against all n strings, each check taking O(m) time
✓ Linear Growth
Space Complexity
O(1)
Only using constant extra space for variables (not counting the output array)
✓ Linear Space
Constraints
- n == arr.length
- 2 ≤ n ≤ 100
- 1 ≤ arr[i].length ≤ 20
- arr[i] consists of lowercase English letters
- All strings are non-empty
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code