Algorithm to remove a character from a word such that the reduced word is still a word in dictionary
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Removing a character from a word so that the resulting string is still a valid dictionary word is a classic problem in algorithm design. It appears in spell checkers, word games, and computational linguistics research. This article presents a clean algorithm for solving this problem, walks through implementations in Python and JavaScript, and analyzes the time and space complexity.
Problem Definition
Given a word and a set of valid dictionary words, find all positions where removing a single character produces a word that exists in the dictionary. For example, given the word "brand" and a dictionary containing "band", "bran", and "brad", removing the 'r' at index 1 yields "band", removing the 'd' at index 4 yields "bran", and removing the 'n' at index 3 yields "brad".
Algorithm Design
The approach is straightforward. For a word of length n, there are exactly n possible single-character removals. Generate each candidate by skipping one character at a time, then check whether the candidate exists in the dictionary. Using a hash set for the dictionary makes each lookup O(1) on average.
Steps:
- Load the dictionary into a hash set for constant-time lookups.
- Iterate through each index
ifrom 0 ton-1. - Build the candidate word by concatenating the substring before index
iwith the substring after indexi. - If the candidate exists in the dictionary set, record it as a valid result.
Python Implementation
Output:
JavaScript Implementation
Recursive Word Reduction Chain
A more interesting variant asks: what is the longest chain of single-character removals where every intermediate word is valid? For example, "starting" could reduce to "staring", then "string", then "sting", then "sing", then "sin", then "in", then "I". This is solvable with depth-first search and memoization.
Complexity Analysis
For the basic single-removal algorithm:
- Time complexity: O(n * k) where
nis the word length andkis the cost of building each candidate string. Since string slicing creates a new string of lengthn-1, the total work is O(n^2). Dictionary lookups are O(1) amortized with a hash set. - Space complexity: O(m) where
mis the dictionary size, for storing the hash set. The results list uses O(n) space in the worst case.
For the recursive chain variant, the time complexity depends on how many valid intermediate words exist. With memoization, each word is computed at most once, so the total work is bounded by O(m * n^2) where m is the number of reachable dictionary words and n is the maximum word length.
Common Pitfalls
Case sensitivity can silently break lookups. If the dictionary contains "Band" but the candidate generates "band", the lookup will fail. Normalize all words to lowercase before processing.
Another issue is handling duplicate results. If a word has repeated characters (like "book"), removing either 'o' produces the same candidate "bok". Depending on your requirements, you may want to deduplicate using a set.
Performance degrades if the dictionary is stored as a list instead of a set. A list lookup is O(m) per query, turning the overall complexity from O(n^2) to O(n * m), which becomes impractical for large dictionaries with hundreds of thousands of entries.
Summary
The word reduction algorithm generates all possible single-character deletions and checks each against a dictionary stored in a hash set. The basic version runs in O(n^2) time and is simple to implement in any language. The recursive chain variant uses DFS with memoization to find the longest sequence of valid reductions. Always use a hash set for the dictionary, normalize case, and handle duplicate candidates to avoid subtle bugs.
Related reading
- algorithm to resolve version scope based dependency
- Algorithm to return all combinations of k elements from n
- Algorithm to rotate an array in linear time
- Algorithm to search for a list of words in a text
- Algorithm to simplify a weighted directed graph of debts
- Algorithm to split an array into P subarrays of balanced sum
- Algorithm to select a set of numbers to reach a minimum total
- Algorithm to select a single, random combination of values?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.