Concatenated Words
To solve this coding challenge of finding all concatenated words in a given list, we'll first detail the approach and then provide precise pseudocode.
Explanation
The aim is to identify words in a list that are comprised entirely of at least two other words from the same list. To do this, we will use Depth-First Search (DFS) to check if a word can be broken down into smaller words that exist in the given list. Here's a detailed breakdown of the approach:- Set Creation : Convert the list of words into a set for O(1) time complexity look-up.
- DFS Utility Function : A recursive function that checks if a given word can be split into smaller words that exist in the word set.
- Main Logic :
- Remove the current word from the set to avoid using the word itself during DFS.
- Use DFS to check if the word can be formed by concatenating other words from the set.
- If it can, add the word to the result list.
- Add the word back to the set for subsequent operations.
- Convert the list of words into a set for quick look-up.
- Implement a DFS function that:
- Iterates through splits of the word.
- Checks if both the prefix and suffix of the split are in the set or can be broken down further.
- In the main function, for each word:
- Temporarily remove the word from the set.
- Use the DFS function to determine if the word can be concatenated from other words in the set.
- If it is a concatenated word, store it in a result list.
- Add the word back to the set after the check.
- findAllConcatenatedWordsInADict Function :
- DFS Helper Function :
- Main Logic in findAllConcatenatedWordsInADict :
Step-by-Step Explanation
Pseudocode with Comments
# Define function to find all concatenated words
function findAllConcatenatedWordsInADict(words):
# Helper function for Depth-First Search (DFS)
function dfs(word, wordSet):
# Loop through possible splits of the word
for i from 1 to length of word:
prefix = substring of word from start to i
suffix = substring of word from i to end
# Check if both parts are in word set
if prefix is in wordSet AND suffix is in wordSet:
return True
# Check recursively if prefix is in wordSet and suffix can be further split
if prefix is in wordSet AND dfs(suffix, wordSet):
return True
return False
# Convert list of words to a set for quick lookup
wordSet = convert words to set
result = empty list
# Iterate over each word in the list
for each word in words:
# Temporarily remove the word from the set
remove word from wordSet
# Check if the word can be formed by other words in the set
if dfs(word, wordSet):
add word to result
# Add the word back to the set
add word to wordSet
return result
Detailed Steps in Pseudocode
-
* Convert the list of words into a set named
wordSet
result
-
* For a given word, iterate from index 1 to the length of the word.
* Split the word into a
prefix
suffix
prefix
suffix
wordSet
prefix
suffix
dfs
True
False
-
* For each word in the initial list:
-
* Temporarily remove the word from
wordSet
dfs
result
wordSet
result