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".

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

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
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