 All Problems
Sequence Reconstruction
medium
topological sort
graph
google
amazon
snapchat

You are given an integer array nums (a permutation of 1 to n) and an array sequences of subsequences. Check if nums is the only shortest supersequence — meaning it's the unique topological sort of the graph built from sequences.

Example 1:

Input: nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
Output: true

Example 2:

Input: nums = [4,1,5,2,6,3], sequences = [[5,2,6,3],[4,1,5,2]]
Output: true

Example 3:

Input: nums = [1,2,3], sequences = [[1,2],[1,3]]
Output: false  (both [1,2,3] and [1,3,2] are valid)

Constraints:

  • 1 <= n <= 10^4
Run to check your code against the sample cases, or submit to run every case