Problem Description
Given an array of unique strings, return all strings that are a substring of another word in the array.
Key Insights
- Iterate over each word and check against every other word in the list.
- Use substring checking (e.g., using "in" in Python, "includes" in JavaScript, or string find methods in C++ and Java).
- The array is small (maximum length = 100) and each string length is bounded (≤ 30), so a simple O(n² * m) solution is acceptable.
- Avoid duplicate checking by ensuring indices are not the same.
Space and Time Complexity
Time Complexity: O(n² * m), where n is the number of words and m is the maximum word length. Space Complexity: O(n) for the result list.
Solution
The approach is to use nested loops: for each word in the array, compare it with every other word to check if it appears as a substring. When a match is found, add the word to the result and break out of the inner loop to avoid redundant checks. This algorithm leverages built-in substring utilities of the programming language used.