Sequence Reconstruction

Given the original sequence org and a list of subsequences seqs, return true if org can be uniquely reconstructed from seqs. Pattern focus: Applications. This checks whether the topological order is unique.

Input Format

org = original sequence, seqs = list of subsequences

Output Format

true if org is the unique reconstruction

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9

Examples

Example 1:

Input:

org = [1,2,3]
seqs = [[1,2],[1,3],[2,3]]

Output:

true

Explanation:

The constraints force the unique order 1 -> 2 -> 3.

Example 2:

Input:

org = [1,2,3]
seqs = [[1,2],[1,3]]

Output:

false

Explanation:

Both 1 -> 2 -> 3 and 1 -> 3 -> 2 satisfy the subsequences, so reconstruction is not unique.

Example 3:

Input:

org = [4,1,5,2,6,3]
seqs = [[5,2,6,3],[4,1,5,2]]

Output:

true

Explanation:

The subsequences enforce the exact original order.

Loading...
Sequence Reconstruction - Topological Sort