LeetCode 3435: Frequencies of Shortest Supersequences Solution

Master LeetCode problem 3435 (Frequencies of Shortest Supersequences), a hard 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.

3435. Frequencies of Shortest Supersequences

Problem Explanation

Explanation

To solve this problem, we can iterate through all possible combinations of words to find the shortest common supersequences (SCS) that are not permutations of each other. For each SCS found, we calculate the frequency of each letter in the lowercase English alphabet. We then store these frequencies in a 2D array.

  1. Generate all possible combinations of words.
  2. For each combination, find the SCS.
  3. Calculate the frequency of each letter in the SCS.
  4. Store the frequency arrays for all valid SCSs.

Time Complexity

The time complexity of this approach is O(2^n * n^2) where n is the number of words.

Space Complexity

The space complexity is O(2^n * 26) for storing the frequency arrays.

Solution Code

class Solution {
    public List<int[]> freqsOfShortestSupersequences(String[] words) {
        List<int[]> result = new ArrayList<>();
        int n = words.length;
        for (int i = 0; i < (1 << n); i++) {
            List<String> subset = new ArrayList<>();
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) > 0) {
                    subset.add(words[j]);
                }
            }
            if (subset.size() > 1 && !arePermutations(subset)) {
                String scs = shortestCommonSupersequence(subset);
                int[] freq = new int[26];
                for (char c : scs.toCharArray()) {
                    freq[c - 'a']++;
                }
                result.add(freq);
            }
        }
        return result;
    }

    private boolean arePermutations(List<String> words) {
        Set<String> set = new HashSet<>();
        for (String word : words) {
            set.add(word);
        }
        return set.size() == 1;
    }

    private String shortestCommonSupersequence(List<String> words) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < words.size(); i++) {
            String word = words.get(i);
            int j = 0;
            while (j < sb.length() && j < word.length() && sb.charAt(j) == word.charAt(j)) {
                j++;
            }
            sb.append(word.substring(j));
        }
        return sb.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3435 (Frequencies of Shortest Supersequences)?

This page provides optimized solutions for LeetCode problem 3435 (Frequencies of Shortest Supersequences) 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 3435 (Frequencies of Shortest Supersequences)?

The time complexity for LeetCode 3435 (Frequencies of Shortest Supersequences) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3435 on DevExCode?

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

Back to LeetCode Solutions