Advertisement

Frequencies of Shortest Supersequences - LeetCode 3435 Solution

Frequencies of Shortest Supersequences - Complete Solution Guide

Frequencies of Shortest Supersequences is LeetCode problem 3435, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array of strings words . Find all shortest common supersequences (SCS) of words that are not permutations of each other. A shortest common supersequence is a string of minimum length that contains each string in words as a subsequence . Return a 2D array of integers freqs that represent all the SCSs. Each freqs[i] is an array of size 26, representing the frequency of each letter in the lowercase English alphabet for a single SCS. You may return the frequency arrays in any order.

Detailed Explanation

The problem asks us to find all shortest common supersequences (SCS) of a given array of strings `words`, where each string in `words` has a length of 2. An SCS is a string of minimal length that contains each string in `words` as a subsequence. The crucial point is to return the frequency of each letter in the lowercase English alphabet for each distinct SCS, avoiding permutations of the same SCS. For example, if the SCSs are "aba" and "bab", the output should be [[1, 2, 0, ...], [2, 1, 0, ...]]. If SCSs are "aac" and "aca", since they are permutations, only keep "aac". The input strings consist of lowercase English letters, and all strings in `words` are unique.

Solution Approach

The solution involves constructing a directed graph from the input `words`, where nodes are characters and edges represent the order between characters within each word. Then, it identifies the strongly connected components (SCCs) within this graph. For each SCC, it determines the possible frequency counts of characters, considering whether the SCC forms an Eulerian circuit. Finally, it combines the frequency counts of different SCCs to generate distinct frequency counts for the shortest common supersequences. The use of Tarjan's algorithm finds SCC, and special handling is added when the SCC is an Eulerian circuit.

Step-by-Step Algorithm

  1. Step 1: Build a directed graph where nodes represent unique characters, and edges (u, v) represent the fact that the word 'uv' exists in the input. Also, store the edges for future use.
  2. Step 2: Find all Strongly Connected Components (SCCs) of the graph using Tarjan's algorithm. This groups characters that must appear in some order together.
  3. Step 3: For each SCC, determine the possible character frequency counts that can form the shortest subsequence for that SCC. This involves two cases: a) If the SCC is of size 1, calculate loops that may occur from edges in the input and set frequencies. b) If the SCC has size > 1, then it needs to check whether it is an Eulerian circuit or not. i) Eulerian Circuit: The characters can appear in multiple sequence due to the circuit itself; thus, add an extra count for each char. ii) Not Eulerian Circuit: Calculate frequencies based on in-degree and out-degree.
  4. Step 4: Combine the possible character frequency counts from all SCCs to build the final character frequency counts for each SCS using combinatorics.
  5. Step 5: Return the distinct frequency counts for the shortest common supersequences.

Key Insights

  • Insight 1: Representing the relationships between characters in `words` as a directed graph is key. Each character becomes a node, and each word represents a directed edge from the first character to the second.
  • Insight 2: Strong Connected Components (SCCs) are crucial. Characters within an SCC must appear together in the SCS, but their relative order matters. If an SCC is an Eulerian circuit, then there are multiple valid sequences. Otherwise, characters within the SCC have dependencies that need to be resolved.
  • Insight 3: Permutations of SCSs must be avoided by only storing unique frequency counts of the SCS strings.
  • Insight 4: Each distinct SCS character composition corresponds to a unique sequence combination, and the minimum composition needs to be found based on graph constraints.

Complexity Analysis

Time Complexity: O(n*m*k)

Space Complexity: O(n*m*k)

Topics

This problem involves: Array, String, Bit Manipulation, Graph, Topological Sort, Enumeration.

Companies

Asked at: PhonePe.