Skip to main content

Concatenated Words - Solution & Explanation

HardArrayStringDynamic ProgrammingDepth-First Search23 min readAsked at: Amazon, eBay, Tiktok
Practice this problem

Problem Statement

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.

 

Example 1:

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

Example 2:

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

 

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.
  • 1 <= sum(words[i].length) <= 105

Approach Overview

Problem Overview: You are given a dictionary of unique words. The task is to return all words that can be formed by concatenating at least two shorter words from the same dictionary. Each word can reuse other dictionary entries, but the concatenated word itself cannot be counted as its own component.

Approach 1: Trie + Depth-First Search (O(N * L^2) time, O(N * L) space)

This approach stores all words in a trie and checks each word using depth-first search. Starting from index 0, traverse the trie character by character. Whenever you reach a terminal node (a complete word), recursively attempt to match the remaining suffix. If the DFS reaches the end of the string after using at least two words, the word is valid. The trie allows fast prefix checks while DFS explores possible split points. This method works well when many words share prefixes because trie traversal avoids repeated substring lookups.

The key idea is treating the word as a sequence of smaller dictionary prefixes. The trie ensures prefix lookups happen in O(L) time, while DFS handles branching splits. Memoization on indices can further reduce repeated work when the same suffix is evaluated multiple times.

Approach 2: Dynamic Programming (O(N * L^2) time, O(L) space per word)

This solution treats the problem like a classic word break variant using dynamic programming. Sort words by length so shorter words are processed first. For each word, build a boolean DP array where dp[i] indicates whether the prefix ending at index i can be formed using previously processed words. Iterate over all split points j < i and check if dp[j] is true and the substring word[j:i] exists in a hash set.

If the final index becomes reachable and the word uses at least two components, it is a concatenated word. After evaluation, add the word to the dictionary so longer words can reuse it. Sorting ensures each word only depends on smaller ones, preventing self-construction. This approach is straightforward and relies heavily on efficient substring membership checks.

Recommended for interviews: The dynamic programming approach is usually expected because it mirrors the classic Word Break pattern and is easier to reason about during an interview. Implementing trie + DFS demonstrates stronger understanding of prefix structures and search strategies, which can be useful when the dictionary has heavy prefix overlap. Showing the DP baseline first and then discussing trie optimization demonstrates strong problem-solving depth.

Approach 1: Trie and Depth-First Search (DFS)

This approach involves using a Trie data structure to store all words. We then perform a DFS to check if a word can be formed using other words in the trie. The main advantage of using a Trie is that it provides an efficient way to store and look up words simply by traversing nodes based on each character of the word.

We will mark nodes in the Trie that represent the end of a word and during DFS checks if the current segment of the word is in the Trie.

In the C solution, we create a Trie with nodes for each letter and a boolean flag indicating the end of a word. We add all the words into the Trie and then use a depth-first search (DFS) function to check if a given word can be constructed by concatenating other words in the Trie. If it can be constructed, it is added to the results list.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * m^2), where n is the number of words and m is the average length of the words. The DFS function can make at most m calls for each word.

Space Complexity: O(n * m) for storing the Trie, where n is the number of words and m is the average length of the words.

Try this approach in the editor →

Approach 2: Dynamic Programming

In this approach, dynamic programming (DP) is used to efficiently solve the problem of finding concatenated words. The idea is to treat each word as a state and store the results of subproblems to avoid repeated calculations. We iterate over each word and for each segment of the word, check if it can be split into valid sub-words based on previously computed results in the DP array.

This can be visualized as using a DP boolean array where each index represents if the word up to that index can be segmented into valid words in the list. This method is beneficial in reducing the number of redundant calculations.

The C solution uses a hashed linked list structure to store words and checks if each word is a concatenated word using dynamic programming. A boolean array is used to record which parts of the word can be formed by other words. The solution attempts to hash words into buckets and checks for matches to segment words.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * m^2), where n is the number of words and m is the average length of the words. The space used for the dp array and hash table is the main contributor.

Space Complexity: O(n * m) due to hash table entry needs.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Trie and Depth-First Search (DFS)

Time Complexity: O(n * m^2), where n is the number of words and m is the average length of the words. The DFS function can make at most m calls for each word.

Space Complexity: O(n * m) for storing the Trie, where n is the number of words and m is the average length of the words.

Dynamic Programming

Time Complexity: O(n * m^2), where n is the number of words and m is the average length of the words. The space used for the dp array and hash table is the main contributor.

Space Complexity: O(n * m) due to hash table entry needs.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Trie + DFSO(N * L^2)O(N * L)Large dictionaries with shared prefixes where trie prefix lookups reduce repeated substring checks
Dynamic Programming (Word Break style)O(N * L^2)O(L)General interview solution; simpler implementation using hash set and prefix DP

Video Solution

Concatenated Words - Leetcode 472 - Python • NeetCodeIO • 26,271 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Concatenated Words easy or hard?
Concatenated Words is classified as a hard problem because it combines multiple concepts: string segmentation, dynamic programming, recursion, and efficient dictionary lookups. Handling edge cases like preventing a word from using itself also increases implementation complexity.
Concatenated Words Python/Java solution
In Python or Java, the typical solution builds a set of words and applies dynamic programming to test each word. Alternatively, a trie combined with DFS checks prefixes and recursively validates the remaining substring. Both implementations achieve roughly O(N * L^2) complexity.
How to solve Concatenated Words in O(n)?
A strict O(n) solution is not typical because each word must evaluate multiple substring splits. The closest practical approach is O(N * L^2) using dynamic programming or trie-based DFS. Optimizations such as memoization and prefix pruning reduce redundant checks but do not eliminate the quadratic dependence on word length.
What is the best approach for Concatenated Words?
The most common solution uses dynamic programming similar to the Word Break problem. Words are processed from shortest to longest, and a DP array checks whether each prefix can be formed from existing dictionary entries. This approach runs in about O(N * L^2) time where N is the number of words and L is the maximum word length.
Is Concatenated Words asked at Google/Amazon/Meta?
Concatenated Words is considered a hard string and dynamic programming problem and appears in advanced interview rounds at companies like Google, Amazon, and Meta. It tests knowledge of word segmentation, trie structures, recursion, and optimization techniques.
What data structure is used in Concatenated Words?
Common data structures include a trie for fast prefix lookup and a hash set for constant-time dictionary membership checks. Dynamic programming arrays are used to track valid word break positions, while DFS recursion explores possible concatenation splits.
What is the time complexity of Concatenated Words?
Most optimal solutions run in O(N * L^2) time. Each word of length L is checked by evaluating multiple split points, and substring membership checks occur against a dictionary or trie. Space complexity ranges from O(L) for DP to O(N * L) when storing all words in a trie.

Ready to solve this problem?

Practice Concatenated Words with our built-in code editor and test cases.

Practice on FleetCode