Find all words in a list that are concatenations of two or more shorter words from it

Quick Overview

Given a list of distinct lowercase words, return every word that can be built by joining two or more shorter words from the same list. It tests string segmentation with dynamic programming, hash set lookups, and keeping a word from matching itself.

Find all words in a list that are concatenations of two or more shorter words from it

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given an array of distinct strings `words`. A concatenated word is a string in `words` that can be written entirely as a concatenation of at least two shorter words from the same array. The shorter words do not have to be distinct, so the same word may appear more than once in a split. Return every concatenated word in `words`. ### Function Signature ```python def find_concatenated_words(words: list[str]) -> list[str]: ``` ### Rules - Return the concatenated words in the order they appear in `words`. - A word cannot be used as a piece of itself, so every piece is strictly shorter than the word being built. - Return an empty list when no word qualifies. ### Constraints - `1 <= len(words) <= 10^4` - `1 <= len(words[i]) <= 30` - `words[i]` consists of lowercase English letters only. - All strings in `words` are unique. - `1 <= sum(len(w) for w in words) <= 10^5` ### Examples ```text Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"] Output: ["catsdogcats","dogcatsdog","ratcatdogcat"] ``` "catsdogcats" is "cats" + "dog" + "cats", "dogcatsdog" is "dog" + "cats" + "dog", and "ratcatdogcat" is "rat" + "cat" + "dog" + "cat". "cats" is left out because "s" is not in the list, and no prefix of "hippopotamuses" is in the list. ```text Input: words = ["word","good","best","work","goodbestwork"] Output: ["goodbestwork"] ``` "goodbestwork" splits into "good" + "best" + "work". The four-letter words cannot be split, because the list has no word shorter than four letters. ```text Input: words = ["a","aa","aaa"] Output: ["aa","aaa"] ``` "aa" is "a" + "a", and "aaa" is "a" + "a" + "a" (or "a" + "aa"), so reusing a piece is allowed. "a" has no shorter word to build it from.

Overview: Given a list of distinct lowercase words, return every word that can be built by joining two or more shorter words from the same list. It tests string segmentation with dynamic programming, hash set lookups, and keeping a word from matching itself.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given an array of distinct strings words. A concatenated word is a string in words that can be written entirely as a concatenation of at least two shorter words from the same array. The shorter words do not have to be distinct, so the same word may appear more than once in a split.

Return every concatenated word in words.

Function Signature

def find_concatenated_words(words: list[str]) -> list[str]:

Rules

  • Return the concatenated words in the order they appear in words .
  • A word cannot be used as a piece of itself, so every piece is strictly shorter than the word being built.
  • Return an empty list when no word qualifies.

Constraints

  • 1 <= len(words) <= 10^4
  • 1 <= len(words[i]) <= 30
  • words[i] consists of lowercase English letters only.
  • All strings in words are unique.
  • 1 <= sum(len(w) for w in words) <= 10^5

Examples

Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]

"catsdogcats" is "cats" + "dog" + "cats", "dogcatsdog" is "dog" + "cats" + "dog", and "ratcatdogcat" is "rat" + "cat" + "dog" + "cat". "cats" is left out because "s" is not in the list, and no prefix of "hippopotamuses" is in the list.

Input: words = ["word","good","best","work","goodbestwork"]
Output: ["goodbestwork"]

"goodbestwork" splits into "good" + "best" + "work". The four-letter words cannot be split, because the list has no word shorter than four letters.

Input: words = ["a","aa","aaa"]
Output: ["aa","aaa"]

"aa" is "a" + "a", and "aaa" is "a" + "a" + "a" (or "a" + "aa"), so reusing a piece is allowed. "a" has no shorter word to build it from.