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: [[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], [1,2,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: "aba" and "bab". 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
Frequencies of Shortest Supersequences INPUT words array: "abc" "ac" Both strings share characters 'a' and 'c' Character Analysis: "abc": a, b, c "ac": a, c [0] [1] ALGORITHM STEPS 1 Find SCS Length Min length containing all 2 Build Supersequence "abc" contains "ac" 3 Check Uniqueness Remove permutations 4 Count Frequencies Map a-z to 0-25 SCS Construction: "abc" SCS = "abc" (length 3) Only 1 unique SCS FINAL RESULT Frequency Array (26 letters): 1 a 1 b 1 c 0 d-z 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]] Status: OK 1 unique SCS found "abc" has a=1, b=1, c=1 All other letters = 0 Key Insight: The Shortest Common Supersequence (SCS) is the shortest string containing all input words as subsequences. Since "ac" is already a subsequence of "abc", the SCS is simply "abc" with frequency [1,1,1,0,...,0]. We filter out permutations to return only unique character frequency distributions. TutorialsPoint - Frequencies of Shortest Supersequences | Optimal Solution
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