Shortest Uncommon Substring in an Array - Problem
You are given an array arr of size n consisting of non-empty strings.
Find a string array answer of size n such that:
answer[i]is the shortest substring ofarr[i]that does not occur as a substring in any other string inarr.- If multiple such substrings exist,
answer[i]should be the lexicographically smallest. - If no such substring exists,
answer[i]should be an empty string.
Return the array answer.
Input & Output
Example 1 — Basic Case
$
Input:
arr = ["cab","ad","bad","c"]
›
Output:
["ab","","ba",""]
💡 Note:
For "cab": "c" appears in "c", "a" appears in "ad" and "bad", "b" appears in "bad", "ca" appears in none, "ab" appears in none (shorter), so "ab". For "ad": "a" appears in "cab" and "bad", "d" appears in "bad", "ad" appears in none but "ad" is the whole string and since we need shortest, we check if any single char works first - none do, so "ad" is not valid as there might be shorter ones. Actually "d" only appears in "bad" and "ad", but "d" appears in "bad" so it's not unique to "ad". Since no substring of "ad" is unique to "ad" only, return empty string.
Example 2 — Single Characters
$
Input:
arr = ["abc","bcd","abcd"]
›
Output:
["a","d","b"]
💡 Note:
For "abc": "a" doesn't appear in "bcd" or "abcd" (wait, "a" appears in "abcd"), so "a" is not unique. Actually "a" appears in "abcd", "b" appears in "bcd" and "abcd", "c" appears in "bcd" and "abcd". Let's check "ab": appears in "abcd". "bc" appears in "bcd" and "abcd". "abc" appears in "abcd". So "abc" has no unique substring - return empty. Wait, let me recalculate: "a" appears in arr[0] and arr[2], "b" appears in all three, "c" appears in all three, "d" appears in arr[1] and arr[2]. So "a" appears in positions 0,2 so count=2. "d" appears in positions 1,2 so count=2. For "abc", we need substring that appears only in "abc" - none of single chars work, let's try "ab" - appears in "abc" and "abcd", so count=2. No unique substring exists for "abc".
Example 3 — Edge Case
$
Input:
arr = ["a","aa","aaa"]
›
Output:
["","aa","aaa"]
💡 Note:
For "a": "a" appears in all strings, so no unique substring exists. For "aa": "a" appears in all, "aa" appears only in "aa" and "aaa", so count=2, not unique. Actually "aa" as substring appears in "aa" (the whole string) and "aaa" (as substring), so not unique to "aa". Wait, this seems wrong. Let me reconsider: "aa" the whole string is same as "aa" substring of "aaa", so "aa" appears in 2 strings. For "aaa": "aaa" appears only in "aaa", so it's unique.
Constraints
- n == arr.length
- 2 ≤ n ≤ 100
- 1 ≤ arr[i].length ≤ 20
- arr[i] consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of strings: ["cab", "ad", "bad", "c"]
2
Process
For each string, find shortest substring not in others
3
Output
Array of unique substrings: ["ab", "ad", "ba", "c"]
Key Takeaway
🎯 Key Insight: Generate substrings by increasing length and find the first one that's unique to each string
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code