Shortest String That Contains Three Strings - Problem

Imagine you're tasked with creating the shortest possible string that contains three given strings as substrings. This is a classic string manipulation problem that tests your understanding of string overlap and optimization.

Given three strings a, b, and c, you need to find a string with minimum length that contains all three strings as substrings. The key insight is that strings can overlap - for example, if you have "abc" and "bcd", you can combine them as "abcd" instead of "abcbcd".

If multiple strings have the same minimum length, return the lexicographically smallest one (the one that would appear first in dictionary order).

Example: If a = "abc", b = "bcd", and c = "def", one possible answer is "abcdef", but we need to check all possible arrangements to find the shortest.

Input & Output

example_1.py โ€” Basic Overlap
$ Input: a = "abc" b = "bcd" c = "def"
โ€บ Output: "abcdef"
๐Ÿ’ก Note: We can merge "abc" and "bcd" as "abcd" (they overlap on 'bc'), then add "def" to get "abcdef". This gives us the shortest possible string of length 6.
example_2.py โ€” Multiple Overlaps
$ Input: a = "ab" b = "bc" c = "ca"
โ€บ Output: "abc"
๐Ÿ’ก Note: The optimal arrangement is "ca" + "ab" + "bc" = "cabc", or "ab" + "bc" + "ca" can be arranged as "abca" or we can find that "abc" contains "ab", "bc" as substrings, and if we arrange optimally, we get "abc" which contains all three as substrings with length 3.
example_3.py โ€” No Overlap
$ Input: a = "abc" b = "def" c = "ghi"
โ€บ Output: "abcdefghi"
๐Ÿ’ก Note: Since there are no overlaps between any of the strings, we simply concatenate them. The lexicographically smallest arrangement is "abc" + "def" + "ghi" = "abcdefghi".

Visualization

Tap to expand
String Superstring PuzzleStep 1: Input PiecesabcbcddefStep 2: Find Overlapsabcbcdoverlap: "bc"Step 3: Merge Resultabcdefโœ“ Length: 6Alternative ArrangementsabcdefghiNo overlapdefabcdLength: 7๐ŸŽฏ Key StrategyTry all 6 arrangements, find maximum overlaps for each,then select the shortest result (lexicographically smallest if tied)
Understanding the Visualization
1
Identify Pieces
We have 3 string pieces that need to be combined into one
2
Find Edge Matches
Look for overlapping patterns between the end of one string and start of another
3
Try Arrangements
Test all 6 possible orderings to see which produces the shortest result
4
Merge Optimally
For each arrangement, merge strings using maximum possible overlaps
5
Select Winner
Choose the shortest result, breaking ties with lexicographical order
Key Takeaway
๐ŸŽฏ Key Insight: With only 3 strings, we can afford to try all arrangements. The magic happens when we find overlaps - shared character sequences that let us merge strings more efficiently than simple concatenation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

6 permutations ร— O(nยฒ) for each merge operation where n is the average string length

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for storing the result strings and temporary merged strings

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค length of each string โ‰ค 1000
  • All strings contain only lowercase English letters
  • Each string is non-empty
  • The total number of possible arrangements is small (6)
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 27
28.4K Views
Medium Frequency
~18 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen