Sequence Reconstruction

Medium
Topological Sort

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 supersequence of all sequences. In other words:

  • nums is the only shortest supersequence of all sequences
  • All sequences are subsequences of nums

Return true if nums is the unique shortest supersequence, 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.

The insight: For nums to be the unique shortest supersequence, the topological sort of the sequences must produce exactly one valid ordering (nums itself). This happens when at every step, there is exactly one node with in-degree 0.

Real-world analogy: Imagine you have partial recordings of events and you want to reconstruct the exact timeline. If the partial recordings uniquely determine the order, there is exactly one valid reconstruction.

Example 1

Input: nums = [1,2,3], sequences = [[1,2],[1,3]]
Output: false
Explanation: We can reconstruct [1,2,3] or [1,3,2]. Since there are multiple valid supersequences, nums is not the unique shortest supersequence.

Example 2

Input: nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
Output: true
Explanation: The only shortest supersequence is [1,2,3]. This is exactly nums.

Example 3

Input: nums = [4,1,5,2,6,3], sequences = [[5,2,6,3],[4,1,5,2]]
Output: true
Explanation: The sequences determine that 5 comes before 2, 2 before 6, 6 before 3, 4 before 1, and 1 before 5. This uniquely determines [4,1,5,2,6,3].

Constraints

  • n == nums.length
  • 1 <= n <= 10^4
  • nums is a permutation of all the integers in the range [1, n]
  • 1 <= sequences.length <= 10^4
  • 1 <= sequences[i].length <= 10^4
  • 1 <= sum(sequences[i].length) <= 10^5
  • 1 <= sequences[i][j] <= n
  • All the arrays of sequences are unique
  • sequences[i] is a subsequence of nums
Show Hints (6)
Hint 1: Build a directed graph from the sequences. What are the nodes and edges?
Hint 2: Adjacent elements in each sequence give us ordering constraints (edges).
Hint 3: When is a topological ordering unique?
Hint 4: If at any step during topological sort there are multiple nodes with in-degree 0, the ordering is not unique.
Hint 5: Use Kahn's algorithm and check that the queue always has exactly one element.
Hint 6: Also verify that the resulting order matches nums exactly.
Loading editor...

Test Results

Click "Run" to execute your code against test cases

AI Tutor

Socratic guidance - I'll ask questions, not give answers

AI Tutor
Hello! I'm your AI tutor, here to guide you through this problem using the Socratic method. I won't give you direct answers, but I'll ask questions and provide hints to help you discover the solution yourself. What's your first instinct when you look at this problem? What approach comes to mind?