Leetcode 2800: Shortest String That Contains Three Strings

grid47
grid47
Exploring patterns and algorithms
Feb 1, 2024 7 min read

Given three strings ‘a’, ‘b’, and ‘c’, find a string that contains all three of these strings as substrings and has the minimum possible length. If there are multiple such strings, return the lexicographically smallest one.
Problem
Approach
Steps
Complexity
Input: You are given three strings, 'a', 'b', and 'c'. Each string consists of lowercase English letters.
Example: Input: a = 'apple', b = 'banana', c = 'orange'
Constraints:
• 1 <= a.length, b.length, c.length <= 100
• a, b, c consist only of lowercase English letters.
Output: Return a string that contains all three strings 'a', 'b', and 'c' as substrings and has the minimum length. If there are multiple valid answers, return the lexicographically smallest one.
Example: Output: 'bananaorange'
Constraints:
Goal: Find the shortest string that contains all three given strings as substrings.
Steps:
• 1. Identify the possible overlapping parts of the strings to minimize the total length.
• 2. Try all permutations of the strings to find the lexicographically smallest solution.
• 3. Combine the strings efficiently using the overlap method and return the result.
Goal: The strings a, b, and c will have lengths ranging from 1 to 100, and will consist of lowercase English letters.
Steps:
• 1 <= a.length, b.length, c.length <= 100
• a, b, c consist only of lowercase English letters.
Assumptions:
• The input strings are non-empty and consist only of lowercase English letters.
Input: Input: a = 'apple', b = 'banana', c = 'orange'
Explanation: The lexicographically smallest string that contains all three strings as substrings is 'bananaorange'.

Input: Input: a = 'cat', b = 'bat', c = 'rat'
Explanation: The shortest and lexicographically smallest string that contains all three strings as substrings is 'catbat'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus