Shortest String That Contains Three Strings - Solution & Explanation
Problem Statement
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
ais lexicographically smaller than a stringb(of the same length) if in the first position whereaandbdiffer, stringahas a letter that appears earlier in the alphabet than the corresponding letter inb. - A substring is a contiguous sequence of characters within a string.
Example 1:
Input: a = "abc", b = "bca", c = "aaa" Output: "aaabca" Explanation: We show that "aaabca" contains all the given strings: a = ans[2...4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and "aaabca" is the lexicographically smallest one.
Example 2:
Input: a = "ab", b = "ba", c = "aba" Output: "aba" Explanation: We show that the string "aba" contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that "aba" is the lexicographically smallest one.
Constraints:
1 <= a.length, b.length, c.length <= 100a,b,cconsist only of lowercase English letters.
Approach Overview
Problem Overview: You are given three strings a, b, and c. Build the shortest possible string that contains all three as substrings. If multiple answers have the same length, return the lexicographically smallest one.
The core challenge is minimizing redundant characters when combining the strings. If the suffix of one string overlaps with the prefix of another, you should merge them instead of concatenating blindly.
Approach 1: Greedy Overlap with Permutation Enumeration (O(n^2) time, O(n) space)
This approach tries every ordering of the three strings (there are only 3! = 6 permutations). For each order, merge strings one by one using maximum overlap. To merge two strings x and y, first check if y already exists inside x. If not, iterate from the largest possible overlap length down to 0 and match the suffix of x with the prefix of y. Append only the non-overlapping part.
Because each overlap check may scan up to the string length, merging costs O(n^2) in the worst case. After generating a merged string for each permutation, choose the shortest result and break ties using lexicographical comparison. The small constant (only six permutations) keeps the solution efficient in practice. This method relies heavily on careful string manipulation and simple greedy decisions about overlaps.
Approach 2: Dynamic Programming with Memoization (O(n^2 * k!) time, O(n * k) space)
A more structured solution models the problem as merging strings while tracking which ones have already been used. Use a bitmask to represent the chosen strings and memoize the best merged result for each state. For each transition, attempt to append a remaining string using the same maximum-overlap merge routine.
The memoization avoids recomputing the best merged result for identical states. While the search space is small for three strings, this approach demonstrates how the idea generalizes to more strings. It combines dynamic programming with substring overlap computation.
Recommended for interviews: The greedy permutation solution is what most interviewers expect. Enumerating all six orders shows you recognize the small input size, and the overlap merge demonstrates practical string manipulation. Mentioning the DP formulation shows deeper understanding, but implementing the greedy overlap method quickly and correctly is usually enough to pass the interview.
Approach 1: Greedy Approach with Overlap
The solution attempts to find maximum overlap between two strings to concatenate them efficiently. We find permutations of a, b, and c to ensure all orders are checked, calculate the overlap, and combine accordingly.
Complexity
Time Complexity: O((n + m + l)^2) for each permutation due to overlap calculation.
Space Complexity: O(n + m + l) for storing combined results and intermediate strings.
Approach 2: Dynamic Programming with Memoization
The C solution initializes a memoization table to store intermediate overlap results, thus minimizing redundant calculations. This assists in efficiently constructing the overlapping substrings.
Complexity
Time Complexity: O(n^3) with reduced constant factors due to memoization.
Space Complexity: O(n^2) for storing DP overlap values.
Approach 3: Enumeration
We enumerate all permutations of the three strings, and for each permutation, we merge the three strings to find the shortest string with the smallest lexicographical order.
The time complexity is O(n^2), and the space complexity is O(n). Where n is the maximum length of the three strings.
Approach 4: Enumeration + KMP
We can use the KMP algorithm to optimize the string merging process.
Time complexity is O(n), and space complexity is O(n). Here, n is the sum of the lengths of the three strings.
Code
Python
Java
C++
Go
TypeScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Greedy Approach with Overlap | Time Complexity: O((n + m + l)^2) for each permutation due to overlap calculation. |
| Dynamic Programming with Memoization | Time Complexity: O(n^3) with reduced constant factors due to memoization. |
| Enumeration | — |
| Enumeration + KMP | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Overlap with Permutation Enumeration | O(n^2) | O(n) | Best practical solution when only three strings are involved |
| Dynamic Programming with Memoization | O(n^2 * k!) | O(n * k) | Useful when extending the problem to more strings or demonstrating DP reasoning |
Video Solution
Leetcode Weekly contest 356 - Medium - Shortest String That Contains Three Strings • Prakhar Agrawal • 2,532 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Shortest String That Contains Three Strings easy or hard?
Shortest String That Contains Three Strings Python/Java solution
How to solve Shortest String That Contains Three Strings in O(n)?
What is the best approach for Shortest String That Contains Three Strings?
Is Shortest String That Contains Three Strings asked at Google/Amazon/Meta?
What data structure is used in Shortest String That Contains Three Strings?
What is the time complexity of Shortest String That Contains Three Strings?
Ready to solve this problem?
Practice Shortest String That Contains Three Strings with our built-in code editor and test cases.
Practice on FleetCode