Shortest String That Contains Three Strings

Given three strings a, b, and c, your task is to find a string that has the minimum length and contains all three strings as substrings.

If there are multiple such strings, return the lexicographically smallest one.

Return a string denoting the answer to the problem.

Notes

  • A string a is lexicographically smaller than a string b of the same length if, in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
  • A substring is a contiguous sequence of characters within a string.
Example 1
Inputa = "abc", b = "bca", c = "aaa"
Output"aaabca"
"aaabca" contains all three strings, has the minimum possible length 6, and is the lexicographically smallest among such strings.
Example 2
Inputa = "ab", b = "ba", c = "aba"
Output"aba"
"aba" contains all three strings, and since c has length 3, the result cannot be shorter.

Constraints

  • 1 <= a.length, b.length, c.length <= 100
  • a, b, c consist 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