> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Concatenated Words Python Solution with Tests

> Tested Python solution for LeetCode 472 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 472, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Depth-First Search](/catalog/topics/depth-first-search), [Trie](/catalog/topics/trie). [View on LeetCode](https://leetcode.com/problems/concatenated-words/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 472   # by problem number
lcpy gen -s concatenated_words   # by problem name
```

## 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.

### Examples

```
Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
```

```
Input: words = ["cat","dog","catdog"]
Output: ["catdog"]
```

### Constraints

* `1 <= words.length <= 10^4`
* `1 <= words[i].length <= 30`
* `words[i]` consists of only lowercase English letters.
* All the strings of `words` are unique.
* `1 <= sum(words[i].length) <= 10^5`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/concatenated_words/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/concatenated_words/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * m^2) where n = len(words), m = max word length
    # Space: O(n * m)
    def find_all_concatenated_words_in_a_dict(self, words: list[str]) -> list[str]:
        word_set = set(words)

        def can_form(word: str) -> bool:
            n = len(word)
            dp = [False] * (n + 1)
            dp[0] = True
            for i in range(1, n + 1):
                for j in range(i):
                    if not dp[j]:
                        continue
                    if j == 0 and i == n:
                        continue
                    if word[j:i] in word_set:
                        dp[i] = True
                        break
            return dp[n]

        return [word for word in words if can_form(word)]
```

## Complexity

| Time | Space |
| - | - |
| O(n \* m^2) where n = len(words), m = max word length | O(n \* m) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.