Talentd
FleetCode

3571. Find the Shortest Superstring II

Easy48.8% acceptancePremium elsewhere, free here

You are given two strings, s1 and s2. Return the shortest possible string that contains both s1 and s2 as substrings. If there are multiple valid answers, return any one of them.

A substring is a contiguous sequence of characters within a string.

Example 1:

Input: s1 = "aba", s2 = "bab"

Output: "abab"

Explanation:

"abab" is the shortest string that contains both "aba" and "bab" as substrings.

Example 2:

Input: s1 = "aa", s2 = "aaa"

Output: "aaa"

Explanation:

"aa" is already contained within "aaa", so the shortest superstring is "aaa".

Constraints:

  • 1 <= s1.length <= 100
  • 1 <= s2.length <= 100
  • s1 and s2 consist of lowercase English letters only.

Topics

Hints open one at a time

Hint 1
Check for the maximum suffix of one string that matches a prefix of the other (in both directions) and merge accordingly.
26:31
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
"aba"
"bab"
Input
"aa"
"aaa"
Your code is saved on this device.Solution