Sequence Reconstruction

Check whether the original sequence original can be uniquely reconstructed from the sequences in seqs.

The org sequence is a permutation of the integers from 1 to n.

Reconstruction means building a shortest common supersequence of the sequences in seqs (i.e., a shortest sequence so that all sequences in seqs are subsequences of it).

Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence.

Parameters

  • original: A list of integers of size n representing the original sequence.
  • seqs: A list of sequences of size m representing the sequences to be reconstructed.

Result

  • true or false, depending on whether the original sequence can be uniquely reconstructed.

Examples

Example 1:

Input: org: [1,2,3], seqs: [[1,2], [1,3]]

Output: false

Explanation:

[1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed.

Example 2:

Input: org: [1,2,3], seqs: [[1,2]]

Output: false

Explanation:

The reconstructed sequence can only be [1,2].

Example 3:

Input: org: [1,2,3], seqs: [[1,2], [1,3], [2,3]]

Output: true

Explanation:

The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct the original sequence [1,2,3].

Example 4:

Input: org: [4,1,5,2,6,3], seqs: [[5,2,6,3], [4,1,5,2]]

Output: true

Constraints

  • 1 <= n <= 10^4
  • 1 <= m <= 10^4
  • 1 <= len(seqs[i]) <= n

Try it yourself

Solution

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade