Frequencies of Shortest Supersequences - Problem
Find All Shortest Common Supersequences
You are given an array of strings
A shortest common supersequence is a string of minimum length that contains each string in
Return a 2D array of integers
Example: For words = ["abc", "ac"], one SCS could be "abc" (frequencies: [1,1,1,0,0,...,0]), but there might be other non-permutation SCS like "abcc" if that's also minimal length.
You are given an array of strings
words. Your task is to find all shortest common supersequences (SCS) of these words that are not permutations of each other.A shortest common supersequence is a string of minimum length that contains each string in
words as a subsequence. Multiple different SCS may exist for the same input.Return a 2D array of integers
freqs where each freqs[i] is an array of size 26, representing the frequency of each letter (a-z) in a unique SCS. You may return the frequency arrays in any order.Example: For words = ["abc", "ac"], one SCS could be "abc" (frequencies: [1,1,1,0,0,...,0]), but there might be other non-permutation SCS like "abcc" if that's also minimal length.
Input & Output
example_1.py โ Basic Case
$
Input:
words = ["abc", "ac"]
โบ
Output:
[[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
๐ก Note:
The shortest common supersequence is "abc" with length 3. It contains both "abc" and "ac" as subsequences. The frequency array shows 1 occurrence each of 'a', 'b', and 'c'.
example_2.py โ Multiple Solutions
$
Input:
words = ["ab", "ba"]
โบ
Output:
[[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
๐ก Note:
Two different shortest supersequences exist: "abc" and "aba". Both have length 3 but different character frequencies, so both are included in the result.
example_3.py โ Single Word
$
Input:
words = ["abc"]
โบ
Output:
[[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
๐ก Note:
When there's only one word, the shortest common supersequence is the word itself. The frequency array represents the character counts in "abc".
Visualization
Tap to expand
Understanding the Visualization
1
Identify Overlaps
Find where input strings can share characters
2
Build DP Table
Calculate minimum lengths for all substring combinations
3
Backtrack Solutions
Enumerate all optimal constructions
4
Generate Frequencies
Convert each unique supersequence to frequency array
Key Takeaway
๐ฏ Key Insight: Use dynamic programming to find minimum length, then backtrack through all optimal construction paths to enumerate unique supersequences
Time & Space Complexity
Time Complexity
O(26^L * n * m)
Where L is the minimum supersequence length, n is number of words, m is average word length
โ Linear Growth
Space Complexity
O(26^L)
Space to store all generated sequences
โ Linear Space
Constraints
- 1 โค words.length โค 8
- 1 โค words[i].length โค 8
- words[i] consists of lowercase English letters only
- All strings in words are unique
- The total number of characters across all words โค 50
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code