Sequence Reconstruction
We are given an integer array nums
, which is a permutation of integers from 1 to n
, and a list of integer sequences. We need to determine if nums
is the only shortest supersequence for all the given sequences.
To check if nums
is the shortest supersequence, we must verify that every sequence is a subsequence of nums
and that no other permutation of nums
could be a valid supersequence.
We can solve this by using the following steps:
- Create an adjacency list: Build a graph where each number in
nums
has a list of numbers that can come after it based on the given sequences. - Check for Validity: Ensure that every sequence in
sequences
is a valid subsequence ofnums
. If not, return False. - Check for Uniqueness: Use the adjacency list to verify that
nums
is the only possible supersequence. If there’s an alternative path, return False.
Here’s the code implementing the above steps:
|
|
This code checks if nums
is the only shortest supersequence for the given sequences by constructing a directed graph and verifying the constraints.