Concatenated Words

HardDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an array of **distinct** non-empty strings words. A word is **concatenated** if it can be written as a concatenation of **two or more** words from the array, where each piece is a word other than the whole word itself. The same piece may be used more than once. Return every concatenated word, in the **same order** they appear in words. Return an empty list if there are none.

Examples

Input: words = ["cat","dog","catdog","dogcatdog","do","g","cow"]
Output: ["dog","catdog","dogcatdog"]
`dog` = `do` + `g`, `catdog` = `cat` + `dog`, and `dogcatdog` = `dog` + `cat` + `dog`. `cow` cannot be split.
Input: words = ["a","b","ab","abc"]
Output: ["ab"]
`ab` = `a` + `b`. `abc` would need a `c` piece, which is not in the list.

Constraints

  • `1 <= words.length <= 10^4`
  • `1 <= words[i].length <= 30`
  • `words[i]` consists of lowercase English letters
  • All strings in `words` are distinct
  • `sum(words[i].length) <= 10^5`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems