Sequence Reconstruction - Problem
Sequence Reconstruction is a fascinating problem that challenges you to determine if a given sequence is the unique shortest supersequence for a set of subsequences.

You're given:
โ€ข An integer array nums of length n where nums is a permutation of integers [1, n]
โ€ข A 2D array sequences where each sequences[i] is a subsequence of nums

Your task is to determine if nums is both the shortest possible and the only supersequence that contains all given subsequences.

Think of it as a puzzle: given several incomplete clues about the order of elements, can you uniquely determine the complete sequence? For example, if sequences = [[1,2],[1,3]], both [1,2,3] and [1,3,2] work as shortest supersequences, so the answer is false. But if sequences = [[1,2],[1,3],[1,2,3]], only [1,2,3] works uniquely!

Input & Output

example_1.py โ€” Basic Unique Case
$ Input: nums = [1,2,3], sequences = [[1,2],[1,3]]
โ€บ Output: false
๐Ÿ’ก Note: Both [1,2,3] and [1,3,2] are valid shortest supersequences. Since nums is not the unique shortest supersequence, return false.
example_2.py โ€” Constrained Unique Case
$ Input: nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
โ€บ Output: true
๐Ÿ’ก Note: The sequences force the order: 1โ†’2, 1โ†’3, and 2โ†’3. This uniquely determines [1,2,3] as the only possible shortest supersequence.
example_3.py โ€” Invalid Reconstruction
$ Input: nums = [4,1,5,2,6,3], sequences = [[5,2,6,3],[4,1,5,2]]
โ€บ Output: false
๐Ÿ’ก Note: The given sequences don't provide enough constraints to uniquely determine nums as the only shortest supersequence.

Constraints

  • 1 โ‰ค nums.length โ‰ค 104
  • nums.length == n
  • nums is a permutation of all integers in range [1, n]
  • 1 โ‰ค sequences.length โ‰ค 104
  • 1 โ‰ค sequences[i].length โ‰ค 104
  • All values in sequences[i] are in range [1, n]
  • sequences[i] are subsequences of nums

Visualization

Tap to expand
Sequences[1,2][1,3][2,3]Graph Edges1 โ†’ 21 โ†’ 32 โ†’ 3In-degreesNode 1: 0Node 2: 1Node 3: 2123Topological Order: 1 โ†’ 2 โ†’ 3Each step has exactly one node with in-degree 0
Understanding the Visualization
1
Build Dependency Graph
From sequences like [1,2] and [2,3], create edges 1โ†’2 and 2โ†’3
2
Count Prerequisites
Calculate in-degree for each node (how many dependencies it has)
3
Process in Order
At each step, exactly one item should be ready (in-degree 0)
4
Verify Uniqueness
If multiple items are ready simultaneously, reconstruction isn't unique
Key Takeaway
๐ŸŽฏ Key Insight: A sequence reconstruction is unique if and only if at each step of topological sorting, exactly one node has in-degree 0. Multiple nodes with in-degree 0 means multiple valid orderings exist.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
28.4K Views
Medium Frequency
~25 min Avg. Time
892 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