444. Sequence Reconstruction
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to verify if a given permutation nums of integers [1, n] is the unique shortest supersequence of a given list of sequences. Each sequence in sequences is a subsequence of nums.
Solution
Sequence Reconstruction in Python
class Solution:
def sequenceReconstruction(self, nums: List[int], sequences: List[List[int]]) -> bool:
n = len(nums)
if n == 0:
return True
# pos[i] stores the index of number i in nums.
# Since numbers are 1 to n, we can use an array of size n+1.
pos = [0] * (n + 1)
for i, num in enumerate(nums):
pos[num] = i
# pairs_covered[i] is true if the pair (nums[i], nums[i+1]) is found.
pairs_covered = [False] * (n - 1)
pairs_to_cover = n - 1
# seen[i] is true if number i has been seen in sequences.
seen = [False] * (n + 1)
seen_count = 0
# is_empty_seq will be true if all sequences are empty, which is not allowed by constraints.
# But as a defensive check, if sequences is not empty but contains only empty lists,
# we need to track if we saw any number.
has_any_number = False
for seq in sequences:
if seq:
has_any_number = True
for i in range(len(seq)):
u = seq[i]
# Check for invalid numbers outside [1,n]
if not (1 <= u <= n):
return False
if not seen[u]:
seen[u] = True
seen_count += 1
if i > 0:
prev = seq[i-1]
# Check if the order in seq is consistent with nums.
if pos[prev] >= pos[u]:
return False # Contradiction to problem statement
# Check if it's an adjacent pair in nums
if pos[prev] + 1 == pos[u]:
if not pairs_covered[pos[prev]]:
pairs_covered[pos[prev]] = True
pairs_to_cover -= 1
if n > 0 and not has_any_number:
return False
# Condition 1: all adjacent pairs in nums must be covered.
# Condition 2: all numbers from 1 to n must be present.
return pairs_to_cover == 0 and seen_count == n
Sequence Reconstruction in Java
class Solution {
public boolean sequenceReconstruction(int[] nums, List<List<Integer>> sequences) {
int n = nums.length;
if (n == 0) return true;
// pos[i] stores the index of number i in nums.
// Since numbers are 1 to n, we can use an array of size n+1.
int[] pos = new int[n + 1];
for (int i = 0; i < n; i++) {
pos[nums[i]] = i;
}
// pairs_covered[i] is true if the pair (nums[i], nums[i+1]) is found.
boolean[] pairs_covered = new boolean[n]; // n-1 pairs, use index 0 to n-2
int pairs_to_cover = n - 1;
// seen[i] is true if number i has been seen in sequences.
boolean[] seen = new boolean[n + 1];
int seen_count = 0;
for (List<Integer> seq : sequences) {
for (int i = 0; i < seq.size(); i++) {
int u = seq.get(i);
// Check for invalid numbers outside [1,n]
if (u < 1 || u > n) return false;
if (!seen[u]) {
seen[u] = true;
seen_count++;
}
if (i > 0) {
int prev = seq.get(i - 1);
// Check if the order in seq is consistent with nums.
if (pos[prev] >= pos[u]) {
return false; // Contradiction to problem statement
}
// Check if it's an adjacent pair in nums
if (pos[prev] + 1 == pos[u]) {
if (!pairs_covered[pos[prev]]) {
pairs_covered[pos[prev]] = true;
pairs_to_cover--;
}
}
}
}
}
// Condition 1: all adjacent pairs in nums must be covered.
// Condition 2: all numbers from 1 to n must be present.
return pairs_to_cover == 0 && seen_count == n;
}
}
Sequence Reconstruction in C++
class Solution {
public:
bool sequenceReconstruction(vector<int>& nums, vector<vector<int>>& sequences) {
int n = nums.size();
if (n == 1) {
// For n=1, nums=[1]. sequences must contain [1] and nothing else that violates.
// The problem constraints make this simple. If any sequence has a number not 1,
// it's not a subsequence of nums. If any seq has length > 1, not a subsequence.
// So sequences must be [[1]], or multiple [[1]]s.
// Let's check if 1 is present in any sequence.
bool found = false;
for (const auto& seq : sequences) {
for (int x : seq) {
if (x == 1) found = true;
if (x < 1 || x > n) return false; // Not a subsequence of nums
}
}
return found;
}
// pos[i] stores the index of number i in nums.
// We use a vector because numbers are 1-indexed.
vector<int> pos(n + 1, 0);
for (int i = 0; i < n; ++i) {
pos[nums[i]] = i;
}
// pairs_covered[i] is true if the pair (nums[i], nums[i+1]) is found.
vector<bool> pairs_covered(n - 1, false);
int pairs_to_cover = n - 1;
// seen[i] is true if number i has been seen in sequences.
vector<bool> seen(n + 1, false);
int seen_count = 0;
for (const auto& seq : sequences) {
for (int i = 0; i < seq.size(); ++i) {
int u = seq[i];
// Check for invalid numbers outside [1,n]
if (u < 1 || u > n) return false;
if (!seen[u]) {
seen[u] = true;
seen_count++;
}
if (i > 0) {
int prev = seq[i-1];
// Check if the order in seq is consistent with nums.
if (pos[prev] >= pos[u]) {
return false; // Contradiction to problem statement
}
// Check if it's an adjacent pair in nums
if (pos[prev] + 1 == pos[u]) {
if (!pairs_covered[pos[prev]]) {
pairs_covered[pos[prev]] = true;
pairs_to_cover--;
}
}
}
}
}
// Condition 1: all adjacent pairs in nums must be covered.
// Condition 2: all numbers from 1 to n must be present.
return pairs_to_cover == 0 && seen_count == n;
}
};
Sequence Reconstruction in TypeScript
function sequenceReconstruction(nums: number[], sequences: number[][]): boolean {
const n = nums.length;
if (n === 0) return true;
// pos[i] stores the index of number i in nums.
const pos: number[] = new Array(n + 1).fill(0);
for (let i = 0; i < n; i++) {
pos[nums[i]] = i;
}
// pairs_covered[i] is true if the pair (nums[i], nums[i+1]) is found.
const pairs_covered: boolean[] = new Array(n).fill(false);
let pairs_to_cover = n - 1;
// seen[i] is true if number i has been seen in sequences.
const seen: boolean[] = new Array(n + 1).fill(false);
let seen_count = 0;
let has_any_number = false;
for (const seq of sequences) {
if (seq.length > 0) {
has_any_number = true;
}
for (let i = 0; i < seq.length; i++) {
const u = seq[i];
// Check for invalid numbers outside [1,n]
if (u < 1 || u > n) return false;
if (!seen[u]) {
seen[u] = true;
seen_count++;
}
if (i > 0) {
const prev = seq[i - 1];
// Check if the order in seq is consistent with nums.
if (pos[prev] >= pos[u]) {
return false; // Contradiction to problem statement
}
// Check if it's an adjacent pair in nums
if (pos[prev] + 1 === pos[u]) {
if (!pairs_covered[pos[prev]]) {
pairs_covered[pos[prev]] = true;
pairs_to_cover--;
}
}
}
}
}
if (n > 0 && !has_any_number) {
return false;
}
// Condition 1: all adjacent pairs in nums must be covered.
// Condition 2: all numbers from 1 to n must be present.
return pairs_to_cover === 0 && seen_count === n;
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.