3076. Shortest Uncommon Substring in an Array
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. And if no such substring exists,answer[i]
should be an empty string.
Return the array answer
.
Example 1:
Example 2:
Constraints:
n == arr.length
2 <= n <= 100
1 <= arr[i].length <= 20
arr[i]
consists only of lowercase English letters.
分析
Substring Generation: We first generate all possible substrings for each string in the input array and count how many times each substring appears across all strings using a dictionary.
Unique Substring Identification: For each string, we then check all possible substrings (starting from the shortest length) to find those that appear exactly once in our count dictionary (meaning they're unique to that string).
Result Selection: For each string, we select the lexicographically smallest substring among the shortest unique substrings found. If no unique substring exists, we return an empty string for that position.
Last updated
Was this helpful?