Sequence Reconstruction - Problem

You are given an integer array nums of length n where nums is a permutation of the integers in the range [1, n]. You are also given a 2D integer array sequences where sequences[i] is a subsequence of nums.

Check if nums is the shortest possible and the only supersequence. The shortest supersequence is a sequence with the shortest length and has all sequences[i] as subsequences.

There could be multiple valid supersequences for the given array sequences. For example, for sequences = [[1,2],[1,3]], there are two shortest supersequences, [1,2,3] and [1,3,2]. While for sequences = [[1,2],[1,3],[1,2,3]], the only shortest supersequence possible is [1,2,3].

Return true if nums is the only shortest supersequence for sequences, or false otherwise.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Input & Output

Example 1 — Unique Sequence
$ Input: nums = [1,2,3], sequences = [[1,2,3],[1]]
Output: true
💡 Note: The sequences force a unique ordering: 1 must come first (from [1]), then 2, then 3 (from [1,2,3]). Only [1,2,3] satisfies all constraints.
Example 2 — Multiple Valid Orderings
$ Input: nums = [1,2,3], sequences = [[1,2],[1,3]]
Output: false
💡 Note: Both [1,2,3] and [1,3,2] satisfy the constraints. Since there are multiple valid shortest supersequences, return false.
Example 3 — Invalid Coverage
$ Input: nums = [4,1,5,2,6,3], sequences = [[5,2,6,3],[4,1,5,2]]
Output: false
💡 Note: The sequences don't provide enough constraints to uniquely determine the order between all elements.

Constraints

  • 1 ≤ n ≤ 104
  • nums is a permutation of [1, n]
  • 1 ≤ sequences.length ≤ 104
  • 1 ≤ sequences[i].length ≤ 104

Visualization

Tap to expand
Sequence Reconstruction INPUT nums = [1, 2, 3] 1 2 3 sequences: seq[0]: 1 2 3 seq[1]: 1 Edges from sequences: 1 --> 2 2 --> 3 DAG Structure: 1 2 3 ALGORITHM STEPS 1 Build Graph Create edges from consecutive elements in each sequence 2 Count In-degrees Track incoming edges for each node in the graph Node: 1 2 3 In-deg: 0 1 1 3 Topological Sort Process queue: must have exactly 1 node at each step 4 Verify Uniqueness Check order matches nums Only 1 valid topological order Queue: [1] --> [2] --> [3] (always size 1 = unique) FINAL RESULT Reconstructed Order: 1 2 3 Matches nums? [1,2,3] OK Unique order? OK Output: true nums is the ONLY shortest supersequence Key Insight: The problem reduces to checking if there's a UNIQUE topological ordering of the graph built from sequences. At each step of BFS/Kahn's algorithm, there must be exactly ONE node with in-degree 0 in the queue. If multiple nodes have in-degree 0 simultaneously, multiple valid orderings exist, so return false. TutorialsPoint - Sequence Reconstruction | Optimal Solution (Topological Sort)
Asked in
Google 45 Facebook 32 Amazon 28
32.4K Views
Medium Frequency
~25 min Avg. Time
1.2K 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