LeetCode 3076: Shortest Uncommon Substring in an Array Solution

Master LeetCode problem 3076 (Shortest Uncommon Substring in an Array), 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.

3076. Shortest Uncommon Substring in an Array

Problem Explanation

Explanation

To solve this problem, we can iterate through each string in the input array and for each string, iterate through all possible substrings. For each substring, we check if it occurs as a substring in any other string in the array. If it does not occur in any other string and it is the shortest (minimum length) substring found so far, we update the answer for that string.

We can use a helper function to check if a substring occurs in any other string efficiently by using a set to store all substrings of each string in the array.

Time Complexity: Let n be the number of strings in the input array and m be the maximum length of a string. The time complexity of this approach is O(n * m^2) as we iterate through all substrings of each string.

Space Complexity: The space complexity is O(n * m^2) to store all possible substrings in the set.

Solution Code

class Solution {
    public String[] findUncommonSubstrings(String[] arr) {
        String[] answer = new String[arr.length];
        Set<String> allSubstrings = new HashSet<>();
        
        for (int i = 0; i < arr.length; i++) {
            answer[i] = "";
            for (int j = 0; j < arr[i].length(); j++) {
                for (int k = j + 1; k <= arr[i].length(); k++) {
                    String substr = arr[i].substring(j, k);
                    if (!allSubstrings.contains(substr)) {
                        boolean foundInOtherStrings = false;
                        for (int l = 0; l < arr.length; l++) {
                            if (l != i && arr[l].contains(substr)) {
                                foundInOtherStrings = true;
                                break;
                            }
                        }
                        if (!foundInOtherStrings && (answer[i].isEmpty() || substr.length() < answer[i].length() || (substr.length() == answer[i].length() && substr.compareTo(answer[i]) < 0))) {
                            answer[i] = substr;
                        }
                    }
                    allSubstrings.add(substr);
                }
            }
        }
        
        return answer;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3076 (Shortest Uncommon Substring in an Array)?

This page provides optimized solutions for LeetCode problem 3076 (Shortest Uncommon Substring in an Array) 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 3076 (Shortest Uncommon Substring in an Array)?

The time complexity for LeetCode 3076 (Shortest Uncommon Substring in an Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3076 on DevExCode?

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

Back to LeetCode Solutions