Frequencies of Shortest Supersequences - Problem
Find All Shortest Common Supersequences

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
Shortest Common Supersequence ConstructionInput Words:"abc""ac"DP Table Construction:Length Calculationdp[i][j] = min length forfirst i chars of word1and first j chars of word2Backtracking:Path EnumerationabcResult:Frequency Array[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]
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

n
2n
โœ“ Linear Growth
Space Complexity
O(26^L)

Space to store all generated sequences

n
2n
โœ“ 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
Asked in
Google 25 Amazon 18 Microsoft 12 Meta 8
15.8K Views
Medium Frequency
~35 min Avg. Time
420 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