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 of arr[i] that does not occur as a substring in any other string in arr.
  • 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.

Example 1
Inputarr = ["cab","ad","bad","c"]
Output["ab","","ba",""]
For "cab", both "ca" and "ab" are shortest unique substrings and "ab" is lexicographically smaller; "ad" and "c" have none, and "bad" has "ba".
Example 2
Inputarr = ["abc","bcd","abcd"]
Output["","","abcd"]
The strings "abc" and "bcd" have no substring absent from the other strings, while "abcd" has shortest uncommon substring "abcd".

Constraints

  • n == arr.length
  • 2 <= n <= 100
  • 1 <= arr[i].length <= 20
  • arr[i] consists only of lowercase English letters.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate