Find all english word substrings of a given string
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding all English-word substrings means scanning a string for every contiguous slice that is also present in a dictionary. The hard part is not the substring generation itself; it is doing the lookup efficiently enough that longer inputs do not explode into unnecessary work.
Start With a Dictionary-Based Scan
If the problem is "find all valid dictionary words that appear as substrings," a dictionary set is the right baseline. A hash set gives constant-time membership checks, which keeps the implementation simple and fast enough for many workloads.
The naive algorithm considers every start index and every end index after it. That produces O(n^2) substrings, and each substring creation can cost additional time. Still, it is a good place to start because it is easy to verify.
The result includes the word plus its position. That matters in practice because repeated words can occur at different offsets, and many downstream tasks need those locations.
Reduce Work With Maximum Word Length
Real dictionaries have a longest word. Once you know that bound, there is no reason to keep extending substrings past it.
This does not change the worst-case shape completely, but it makes the scan much cheaper when the input is long and the word list contains reasonably short entries.
Use a Trie When the Dictionary Is Large
A trie helps when you have a large word list and you want to stop early as soon as a prefix is impossible. Instead of slicing every possible substring and asking "is this a word," you walk forward character by character and abandon the search the moment a prefix no longer exists.
The trie version pays an upfront build cost, but it avoids a large number of hopeless substring checks.
Decide Whether You Need Substrings or Segmentation
This question is often confused with word breaking. Substring search asks for any contiguous English word anywhere inside the string. Word segmentation asks whether the whole string can be split into valid words, often with no unused characters.
For example, in "catsanddog", the substrings "cat", "cats", "sand", "and", and "dog" are all valid findings. A segmenter would go further and try to assemble a complete parse such as "cats" + "and" + "dog".
If your real goal is segmentation, dynamic programming is usually a better fit than raw substring enumeration.
Handling Normalization
Dictionary lookups are only as good as your normalization rules. English text may contain uppercase letters, apostrophes, hyphens, and punctuation. You should define whether "don't", "co-op", or "e-mail" count as words, then normalize both the dictionary and the input the same way.
If you skip that step, the algorithm can look correct while silently missing valid matches.
Common Pitfalls
The most common pitfall is using a list for the dictionary instead of a set or trie. That turns each lookup into a linear scan and makes the whole algorithm much slower.
Another issue is creating substrings repeatedly in languages where slicing copies data. In performance-sensitive code, consider walking indices or prefixes rather than allocating new strings for every candidate.
Case handling is also easy to get wrong. If the input is lowercased but the dictionary is not, some matches disappear for no obvious reason.
Finally, make sure the requirement really is "all substrings." If the caller wants only the longest matches, only distinct words, or only segmentations that cover the full string, the implementation should change accordingly.
Summary
- A hash-set dictionary is the simplest correct solution for substring lookup.
- Bounding the search by maximum dictionary word length avoids pointless checks.
- A trie is useful when the dictionary is large and prefix pruning matters.
- Substring search and full-word segmentation are different problems.
- Normalize input and dictionary consistently or valid words will be missed.

