Concatenated Words - Problem
Given an array of strings words (without duplicates), return all the concatenated words in the given list of words.
A concatenated word is defined as a string that is comprised entirely of at least two shorter words (not necessarily distinct) in the given array.
Note: The shorter words used to form the concatenated word must exist in the original array.
Input & Output
Example 1 — Multiple Concatenated Words
$
Input:
words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
›
Output:
["catsdogcats","dogcatsdog","ratcatdogcat"]
💡 Note:
"catsdogcats" = "cats" + "dog" + "cats", "dogcatsdog" = "dog" + "cats" + "dog", "ratcatdogcat" = "rat" + "cat" + "dog" + "cat"
Example 2 — No Concatenated Words
$
Input:
words = ["cat","dog","catdog"]
›
Output:
["catdog"]
💡 Note:
"catdog" can be formed from "cat" + "dog", which are both in the array
Example 3 — Edge Case with Single Word
$
Input:
words = ["a"]
›
Output:
[]
💡 Note:
No word can be formed from at least two other words since there's only one word
Constraints
- 1 ≤ words.length ≤ 104
- 1 ≤ words[i].length ≤ 30
- words[i] consists of only lowercase English letters
- All the strings of words are unique
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code