Given an array of string words, return all strings in words that are a substring of another word. You can return the answer in any order.
Example 1:
Input: words = ["mass","as","hero","superhero"] Output: ["as","hero"] Explanation: "as" is substring of "mass" and "hero" is substring of "superhero". ["hero","as"] is also a valid answer.
Example 2:
Input: words = ["leetcode","et","code"] Output: ["et","code"] Explanation: "et", "code" are substring of "leetcode".
Example 3:
Input: words = ["blue","green","bu"] Output: [] Explanation: No string of words is substring of another string.
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 30words[i] contains only lowercase English letters.words are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method checks every single word in the list to see if it's contained in every other word. It's like comparing each word against all the others, one by one, to find matches.
Here's how the algorithm would work step-by-step:
def string_matching_in_array(words):
matching_words = []
number_of_words = len(words)
for i in range(number_of_words):
for j in range(number_of_words):
# Ensure we don't compare a word with itself.
if i != j:
# Check if word at index i is a substring of word at index j.
if words[i] in words[j]:
# Only add if it's not already in the matching words list
if words[i] not in matching_words:
matching_words.append(words[i])
return matching_wordsThe goal is to find strings within an array that are contained inside other strings. The most efficient approach avoids unnecessary comparisons by first ordering the strings and then checking if shorter strings are inside longer ones.
Here's how the algorithm would work step-by-step:
def string_matching_in_array(words):
words.sort(key=len)
result = []
for i in range(len(words)):
# Iterate through the sorted list of words.
for j in range(i + 1, len(words)):
# Only check against longer strings to avoid redundant comparisons.
if words[i] in words[j]:
result.append(words[i])
break
# Avoid re-adding if found in multiple words
return result| Case | How to Handle |
|---|---|
| Input array is null or undefined | Throw an IllegalArgumentException or return an empty list to prevent NullPointerException. |
| Input array is empty | Return an empty list immediately as there are no strings to compare. |
| Input array contains only one string | Return an empty list since a string needs at least another to be a substring of it. |
| Input array contains empty strings | Handle empty strings carefully; an empty string is a substring of every string, so it should be added to the result only if other strings exist and if it is not itself one of the others substring. |
| Long strings causing performance issues | Consider using more efficient substring search algorithms like Knuth-Morris-Pratt (KMP) or Boyer-Moore for very long strings. |
| Array contains duplicate strings | The algorithm should still function correctly, identifying substring relationships even if the same string appears multiple times. |
| One string is equal to another string | Handle equality; a string is not considered a substring of itself in this case, so do not include it in the output. |
| Maximum array size exceeds memory limitations | Consider breaking the input into smaller chunks or using external storage/processing if the array is too large to fit in memory. |