Ctrl K

Words Hidden in Others

Easy
You are given a list of words words. Return every element that appears as a contiguous piece inside some other element of the same list. "Other" means an element at a different position: if the same word is listed twice, each copy is contained in the other and both belong to the answer. Each qualifying element is reported as many times as it occurs in words. The answer may come in any order.
Example 1
words=['sunflower', 'flow', 'sun', 'rain'] → ['flow', 'sun']

"flow" and "sun" both appear inside "sunflower", while "rain" and "sunflower" itself are not inside any other word.

Example 2
words=['cat', 'dog', 'bird'] → []

No word hides inside another, so the answer is empty.

Example 3
words=['ice', 'rice', 'price'] → ['ice', 'rice']

"ice" sits in both "rice" and "price", and "rice" sits in "price". Both qualify, each listed once.

Constraints
  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 30
  • words[i] consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords With a Given Start EasyBookend Word Pairs EasyHighlight Search Hits MediumHighlight Search Terms MediumCapitals Follow the Pattern Medium
loading Python…
Input
words=['sunflower', 'flow', 'sun', 'rain']
Expected
['flow', 'sun']

Run checks the first 3 tests in your browser. Submit checks all 92 tests on the server, hidden ones included.