LeetCode 444: Sequence Reconstruction Solution

Master LeetCode problem 444 (Sequence Reconstruction), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

444. Sequence Reconstruction

Problem Explanation

Explanation:

The problem asks us to determine whether a given sequence org can be reconstructed from a set of sequences seqs. In order to reconstruct the sequence, the following conditions must be satisfied:

  • The sequence org must be a permutation of the integers from 1 to n, where n is the number of elements in org.
  • The sequences in seqs must maintain their relative order in org.

To solve this problem, we can perform the following steps:

  1. Create a directed graph where each number in org is a node.
  2. Add edges to the graph based on the sequences in seqs.
  3. Perform a topological sort on the graph and check if the resulting order matches org.

: :

Solution Code

import java.util.*;

class Solution {
    public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
        Map<Integer, Set<Integer>> graph = new HashMap<>();
        Map<Integer, Integer> indegree = new HashMap<>();

        for (List<Integer> seq : seqs) {
            for (int i = 0; i < seq.size(); i++) {
                graph.putIfAbsent(seq.get(i), new HashSet<>());
                indegree.putIfAbsent(seq.get(i), 0);

                if (i > 0) {
                    if (graph.get(seq.get(i - 1)).add(seq.get(i))) {
                        indegree.put(seq.get(i), indegree.get(seq.get(i)) + 1);
                    }
                }
            }
        }

        if (org.length != indegree.size()) {
            return false;
        }

        Queue<Integer> queue = new LinkedList<>();
        for (int num : indegree.keySet()) {
            if (indegree.get(num) == 0) {
                queue.offer(num);
            }
        }

        int index = 0;
        while (!queue.isEmpty()) {
            if (queue.size() > 1) {
                return false;
            }

            int current = queue.poll();
            if (org[index++] != current) {
                return false;
            }

            for (int neighbor : graph.get(current)) {
                indegree.put(neighbor, indegree.get(neighbor) - 1);
                if (indegree.get(neighbor) == 0) {
                    queue.offer(neighbor);
                }
            }
        }

        return index == org.length;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 444 (Sequence Reconstruction)?

This page provides optimized solutions for LeetCode problem 444 (Sequence Reconstruction) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 444 (Sequence Reconstruction)?

The time complexity for LeetCode 444 (Sequence Reconstruction) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 444 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 444 in Java, C++, or Python.

Back to LeetCode Solutions