271. Shortest Superstring II

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 the smallest 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".

Now Your Turn!

Pick the correct output for the given input

Input: s1 = "a", s2 = "b"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
string shortestSuperstring(string s1, string s2) {
}
};
Test Case

Input:

S1
S2