String Matching in an Array

Given an array of strings words, return all strings in words that are a substring of another word.

You can return the answer in any order.

Example 1
Inputwords = ["mass","as","hero","superhero"]
Output["as","hero"]
"as" is a substring of "mass" and "hero" is a substring of "superhero"; ["hero", "as"] is also a valid answer.
Example 2
Inputwords = ["leetcode","et","code"]
Output["et","code"]
"et" and "code" are substrings of "leetcode".

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 30
  • words[i] contains only lowercase English letters.
  • All the strings of words are unique.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate