LeetCode Problem Workspace

Frequencies of Shortest Supersequences

Compute all unique shortest common supersequences of given words using graph indegree tracking and topological ordering efficiently.

category

6

Topics

code_blocks

0

Code langs

hub

3

Related

Practice Focus

Hard · Graph indegree plus topological ordering

bolt

Answer-first summary

Compute all unique shortest common supersequences of given words using graph indegree tracking and topological ordering efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Graph indegree plus topological ordering

Try AiBox Copilotarrow_forward

Use graph indegree combined with topological sorting to construct all shortest common supersequences. Track each character placement while pruning permutations to avoid duplicates. Map each resulting SCS to a 26-length frequency array to return all valid outputs efficiently.

Problem Statement

Given an array of unique strings words, determine all shortest common supersequences (SCS) that are not permutations of each other. Each SCS is a minimal-length string containing every word in words as a subsequence, respecting character order within words.

Return a 2D array freqs where each row represents one SCS as a frequency array of size 26 for lowercase English letters. The frequency arrays may be returned in any order. The words array contains strings of length 2 with at most 16 unique letters overall, and 1 <= words.length <= 256.

Examples

Example 1

Input: words = ["ab","ba"]

Output: [[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],[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]]

The two SCSs are "aba" and "bab" . The output is the letter frequencies for each one.

Example 2

Input: words = ["aa","ac"]

Output: [[2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

The two SCSs are "aac" and "aca" . Since they are permutations of each other, keep only "aac" .

Example 3

Input: words = ["aa","bb","cc"]

Output: [[2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

"aabbcc" and all its permutations are SCSs.

Constraints

  • 1 <= words.length <= 256
  • words[i].length == 2
  • All strings in words will altogether be composed of no more than 16 unique lowercase letters.
  • All strings in words are unique.

Solution Approach

Graph Construction and Indegree Tracking

Build a directed graph where each node is a character and edges represent ordering constraints from the input words. Maintain an indegree array to track dependencies and identify characters ready to be placed in the SCS, ensuring no character is used more than twice per SCS.

Topological Ordering with Backtracking

Use backtracking combined with topological ordering to generate all valid sequences. Select nodes with zero indegree, append to the current SCS, decrement indegrees, recurse, then restore indegrees. Prune branches producing sequences identical under permutation to previous SCSs.

Mapping SCS to Frequency Arrays

Once a valid SCS is formed, convert it to a 26-length frequency array. Collect all unique frequency arrays in a list and return. This avoids storing full strings and handles permutations automatically by counting letters.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity depends on graph branching factor and backtracking over all valid topological orders; space complexity depends on storing partial SCS sequences and frequency arrays. Worst case grows exponentially with number of characters but is limited by max 16 unique letters and length constraints.

What Interviewers Usually Probe

  • Expect questions about graph indegree updates and how zero-indegree nodes are selected.
  • Look for understanding of pruning identical permutations to reduce computation.
  • May ask why each character appears at most twice in an SCS and how that limits graph size.

Common Pitfalls or Variants

Common pitfalls

  • Not pruning SCS permutations can explode memory and runtime.
  • Incorrectly updating indegrees during backtracking can skip valid sequences.
  • Assuming words can be concatenated arbitrarily without respecting subsequence constraints.

Follow-up variants

  • Compute SCS frequencies for words of length >2 while limiting character repetitions.
  • Return the actual SCS strings instead of frequency arrays.
  • Handle SCS computation when words may repeat or have overlapping letters more than twice.

FAQ

What is the shortest common supersequence in this problem?

It is the minimal-length string containing all input words as subsequences, respecting character order.

Why does each character appear at most twice in the SCS?

Because words are length 2 and unique letters are limited, the SCS cannot need more than two occurrences of any letter.

How does graph indegree plus topological ordering apply here?

It models character dependencies from words, letting you generate all valid sequences by selecting zero-indegree nodes recursively.

Can output frequency arrays be in any order?

Yes, as long as all unique SCS frequency arrays are included, their order in the 2D array does not matter.

What happens if multiple SCSs are permutations of each other?

Only one representative frequency array is kept to avoid duplicates, discarding other permutations.

terminal

Solution

Solution 1

#### Python3

1
Frequencies of Shortest Supersequences Solution: Graph indegree plus topological order… | LeetCode #3435 Hard