🧠
Brute Force (Check All Possible Supersequences)
💡 This approach tries to generate all possible sequences that can be formed from seqs and checks if org is the only one. It is impractical but helps understand the problem deeply.
Intuition
If we try to build all sequences that satisfy the subsequence constraints, we can check if only org matches. This brute force approach enumerates all possible sequences.
Algorithm
- Generate all permutations of numbers from 1 to n.
- For each permutation, check if all sequences in seqs are subsequences of it.
- Count how many permutations satisfy the subsequence condition and match org.
- Return true if exactly one permutation matches org, else false.
💡 This approach is conceptually simple but computationally infeasible for large n due to factorial permutations.
from itertools import permutations
def sequenceReconstruction(org, seqs):
n = len(org)
nums = list(range(1, n+1))
count = 0
for perm in permutations(nums):
if all(is_subsequence(perm, seq) for seq in seqs):
if list(perm) == org:
count += 1
if count > 1:
return False
return count == 1
def is_subsequence(seq, sub):
it = iter(seq)
return all(x in it for x in sub)
# Driver code
if __name__ == '__main__':
org = [1,2,3]
seqs = [[1,2],[1,3]]
print(sequenceReconstruction(org, seqs)) # Expected: False
Line Notes
from itertools import permutationsImport permutations to generate all possible sequences
for perm in permutations(nums):Try every possible ordering of numbers 1 to n
if all(is_subsequence(perm, seq) for seq in seqs):Check if current permutation contains all seqs as subsequences
if count > 1:Early stop if more than one valid sequence found
import java.util.*;
public class Solution {
public static boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
int n = org.length;
List<Integer> nums = new ArrayList<>();
for (int i = 1; i <= n; i++) nums.add(i);
int[] count = {0};
permute(nums, 0, org, seqs, count);
return count[0] == 1;
}
private static void permute(List<Integer> nums, int start, int[] org, List<List<Integer>> seqs, int[] count) {
if (count[0] > 1) return;
if (start == nums.size()) {
if (allSubseq(nums, seqs)) {
if (matchesOrg(nums, org)) count[0]++;
}
return;
}
for (int i = start; i < nums.size(); i++) {
Collections.swap(nums, start, i);
permute(nums, start + 1, org, seqs, count);
Collections.swap(nums, start, i);
}
}
private static boolean allSubseq(List<Integer> perm, List<List<Integer>> seqs) {
for (List<Integer> seq : seqs) {
if (!isSubsequence(perm, seq)) return false;
}
return true;
}
private static boolean isSubsequence(List<Integer> perm, List<Integer> seq) {
int i = 0, j = 0;
while (i < perm.size() && j < seq.size()) {
if (perm.get(i).equals(seq.get(j))) j++;
i++;
}
return j == seq.size();
}
private static boolean matchesOrg(List<Integer> perm, int[] org) {
for (int i = 0; i < org.length; i++) {
if (!perm.get(i).equals(org[i])) return false;
}
return true;
}
// Driver code
public static void main(String[] args) {
int[] org = {1,2,3};
List<List<Integer>> seqs = Arrays.asList(Arrays.asList(1,2), Arrays.asList(1,3));
System.out.println(sequenceReconstruction(org, seqs)); // Expected: false
}
}
Line Notes
permute(nums, 0, org, seqs, count);Start generating all permutations from index 0
if (count[0] > 1) return;Stop early if more than one valid sequence found
if (allSubseq(nums, seqs)) {Check if current permutation contains all seqs as subsequences
if (matchesOrg(nums, org)) count[0]++;Increment count only if permutation matches original sequence
#include <bits/stdc++.h>
using namespace std;
bool isSubsequence(const vector<int>& perm, const vector<int>& seq) {
int i = 0, j = 0;
while (i < (int)perm.size() && j < (int)seq.size()) {
if (perm[i] == seq[j]) j++;
i++;
}
return j == (int)seq.size();
}
bool allSubseq(const vector<int>& perm, const vector<vector<int>>& seqs) {
for (const auto& seq : seqs) {
if (!isSubsequence(perm, seq)) return false;
}
return true;
}
bool matchesOrg(const vector<int>& perm, const vector<int>& org) {
for (int i = 0; i < (int)org.size(); i++) {
if (perm[i] != org[i]) return false;
}
return true;
}
void permute(vector<int>& nums, int start, const vector<int>& org, const vector<vector<int>>& seqs, int& count) {
if (count > 1) return;
if (start == (int)nums.size()) {
if (allSubseq(nums, seqs) && matchesOrg(nums, org)) {
count++;
}
return;
}
for (int i = start; i < (int)nums.size(); i++) {
swap(nums[start], nums[i]);
permute(nums, start + 1, org, seqs, count);
swap(nums[start], nums[i]);
}
}
int main() {
vector<int> org = {1,2,3};
vector<vector<int>> seqs = {{1,2},{1,3}};
vector<int> nums = {1,2,3};
int count = 0;
permute(nums, 0, org, seqs, count);
cout << (count == 1 ? "true" : "false") << endl; // Expected: false
return 0;
}
Line Notes
void permute(vector<int>& nums, int start, const vector<int>& org, const vector<vector<int>>& seqs, int& count)Recursive function to generate permutations
if (count > 1) return;Stop recursion early if more than one valid sequence found
if (allSubseq(nums, seqs) && matchesOrg(nums, org)) {Check if current permutation contains all seqs as subsequences and matches org
count++;Increment count for each valid permutation matching org
function isSubsequence(perm, seq) {
let i = 0, j = 0;
while (i < perm.length && j < seq.length) {
if (perm[i] === seq[j]) j++;
i++;
}
return j === seq.length;
}
function allSubseq(perm, seqs) {
for (const seq of seqs) {
if (!isSubsequence(perm, seq)) return false;
}
return true;
}
function matchesOrg(perm, org) {
for (let i = 0; i < org.length; i++) {
if (perm[i] !== org[i]) return false;
}
return true;
}
function permute(nums, start, org, seqs, count) {
if (count.count > 1) return;
if (start === nums.length) {
if (allSubseq(nums, seqs) && matchesOrg(nums, org)) {
count.count++;
}
return;
}
for (let i = start; i < nums.length; i++) {
[nums[start], nums[i]] = [nums[i], nums[start]];
permute(nums, start + 1, org, seqs, count);
[nums[start], nums[i]] = [nums[i], nums[start]];
}
}
// Driver code
const org = [1,2,3];
const seqs = [[1,2],[1,3]];
const nums = [1,2,3];
const count = {count: 0};
permute(nums, 0, org, seqs, count);
console.log(count.count === 1); // Expected: false
Line Notes
function permute(nums, start, org, seqs, count)Recursive function to generate all permutations
if (count.count > 1) return;Stop recursion early if more than one valid sequence found
if (allSubseq(nums, seqs) && matchesOrg(nums, org)) {Check if current permutation contains all seqs as subsequences and matches org
count.count++;Increment count for each valid permutation matching org
TimeO(n! * m * k) where n is length of org, m is number of sequences, k average length of sequences
SpaceO(n) for permutation storage and recursion stack
We generate all permutations (n!) and check subsequences for each, which is very expensive.
💡 For n=10, n! is 3,628,800, which is too large to compute in reasonable time.
Interview Verdict: TLE
This approach is only useful to understand the problem and why we need better methods; it is not practical for interviews.