Advertisement

Shortest Uncommon Substring in an Array - LeetCode 3076 Solution

Shortest Uncommon Substring in an Array - Complete Solution Guide

Shortest Uncommon Substring in an Array is LeetCode problem 3076, a Medium 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 arr of size n consisting of non-empty strings. Find a string array answer of size n such that: answer[i] is the shortest substring of arr[i] that does not occur as a substring in any other string in arr . If multiple such substrings exist, answer[i] should be the lexicographically smallest . And if no such substring exists, answer[i] should be an empty string. Return the array answer . Example 1: Input: arr = ["cab","ad","bad","c"] Output: ["ab","","ba",""] Explanation: We

Detailed Explanation

The problem requires finding, for each string in an input array `arr`, the shortest substring that is unique to that string, meaning it doesn't appear as a substring in any other string in `arr`. If multiple such substrings of the same length exist, the lexicographically smallest one should be selected. If no such substring exists, the result for that string should be an empty string. The output should be an array of strings, where each element corresponds to the solution for the corresponding string in the input array.

Solution Approach

The provided solution constructs a map (or dictionary) where keys are substrings and values are sets of indices of strings in which those substrings appear. Then, for each string in the input array, it iterates through its substrings, checking if the substring's index set in the map contains only the index of the current string. If so, it means the substring is unique to that string. If multiple unique substrings of the same length are found, the lexicographically smallest one is chosen. The algorithm finds the shortest unique substring for each string in the array and returns the array of these substrings.

Step-by-Step Algorithm

  1. Step 1: Initialize a map (substring_indices) to store substrings as keys and sets of string indices as values. This map will track in which strings each substring appears.
  2. Step 2: Iterate through each string `s` in the input array `arr` at index `i`.
  3. Step 3: Generate all substrings of `s` with lengths ranging from 1 to the length of `s`.
  4. Step 4: For each substring, add the index `i` to the set of indices associated with that substring in the `substring_indices` map.
  5. Step 5: After processing all strings and their substrings, initialize an empty array `answer` to store the results.
  6. Step 6: Iterate through each string `s` in the input array `arr` again, this time to find the shortest unique substring.
  7. Step 7: For each string `s`, iterate through its substrings (again, in increasing order of length).
  8. Step 8: Check if the current substring appears in the `substring_indices` map. If it does, check the size of the associated index set. If the size of the index set is 1 (meaning the substring appears only in the current string), add the substring to a list of `candidates`.
  9. Step 9: If any `candidates` are found, select the lexicographically smallest substring and add it to the `answer` array. Break the inner loop to ensure that the shortest substring is chosen.
  10. Step 10: If no unique substring is found for a string, add an empty string to the `answer` array.
  11. Step 11: Return the `answer` array.

Key Insights

  • Insight 1: The core idea is to efficiently identify all substrings of each string and track their occurrences across all strings in the input array.
  • Insight 2: A hash table (map or dictionary) is ideal for storing substrings and the indices of the strings they appear in. This allows for quick lookup of substring occurrences.
  • Insight 3: Iterating through substrings in increasing order of length allows for early termination when a unique substring is found, ensuring we find the *shortest* unique substring first. Lexicographical ordering can be done by simply using min() on a list of candidate strings of equal length.
  • Insight 4: Caching already seen substrings of a string during substring generation avoids redundant calculations and insertions into the hash table, improving efficiency.

Complexity Analysis

Time Complexity: O(N * L^3)

Space Complexity: O(N * L^2)

Topics

This problem involves: Array, Hash Table, String, Trie.

Companies

Asked at: Affirm, Airbnb, Moveworks.