Intelligent spell checking, auto-completion, and correction suggestions
A sophisticated spell checker powered by Trie (Prefix Tree) data structure. Features real-time spell checking, intelligent auto-completion, and smart correction suggestions using edit distance algorithms.
|
|
root
/ | \
h c p
/ | \
e a r
/ | \
l t o
/ | \
l * g
/ \
o r
/ \
* a
\
m
\
*
Words: "hello", "cat", "program"
(*) = End of word marker
class TrieNode {
bool isEndOfWord; // Marks complete words
unordered_map<char, TrieNode*> mp; // Children nodes (26 letters max)
};
class Trie {
TrieNode* root;
// Insert, Search, Delete, AutoComplete, Suggestions
};| Operation | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Insert | O(L) | O(L) | L = word length |
| Search | O(L) | O(1) | Faster than hash table for prefixes |
| Delete | O(L) | O(1) | Marks word as invalid |
| Auto-Complete | O(L + N × M) | O(N × M) | N = results, M = avg word length |
| Suggestions | O(L² × 26) | O(L² × 26) | Generates all 1-edit candidates |
| Space (Total) | O(ALPHABET × N × M) | — | N = words, M = avg length |
bool searchTrie(const string& word) {
TrieNode* crawler = root;
for (char c : word) {
if (crawler->mp.find(c) == crawler->mp.end())
return false; // Character not found
crawler = crawler->mp[c];
}
return crawler->isEndOfWord; // Must be complete word
}Flow: hello → h → e → l → l → o → ✓ (isEndOfWord = true)
vector<string> autoComplete(const string& prefix) {
// 1. Navigate to prefix node
TrieNode* crawler = root;
for (char c : prefix) {
if (!crawler->mp.count(c)) return {}; // Prefix doesn't exist
crawler = crawler->mp[c];
}
// 2. DFS to collect all words from this node
vector<string> results;
findWords(crawler, prefix, results); // Recursive traversal
return results;
}Example: prog → Finds all words starting with "prog"
vector<string> getSuggestions(const string& word) {
unordered_set<string> candidates;
// Generate all possible 1-edit variations
for (int i = 0; i < word.length(); i++) {
// Deletion: "helo" → "hlo", "elo", "heo", "hel"
candidates.insert(word.substr(0, i) + word.substr(i+1));
// Substitution: "helo" → "aelo", "belo", ..., "helz"
for (char c = 'a'; c <= 'z'; c++) {
string temp = word;
temp[i] = c;
candidates.insert(temp);
}
// Transposition: "helo" → "ehlo", "hleo", "heol"
if (i < word.length() - 1) {
string temp = word;
swap(temp[i], temp[i+1]);
candidates.insert(temp);
}
}
// Insertion: "helo" → "ahelo", "haelo", ..., "heloz"
for (int i = 0; i <= word.length(); i++) {
for (char c = 'a'; c <= 'z'; c++) {
candidates.insert(word.substr(0, i) + c + word.substr(i));
}
}
// Filter: Keep only valid dictionary words
vector<string> valid;
for (const string& candidate : candidates) {
if (searchTrie(candidate)) valid.push_back(candidate);
}
return valid;
}Candidate Count: ~54L + 26L + 26(L+1) = ~106L candidates (L = word length)
$ ./spell_checker
Enter The FileName:
dictionary.txt
Smart Spell Checker Ready!
Dictionary loaded successfully.
Type words or prefixes (type 'exit' to quit):
> hello
✓ 'hello' is spelled correctly!
> helo
✗ 'helo' not found.
Did you mean:
- hello
- help
> prog
Auto-completions for 'prog':
- program
> programing
✗ 'programing' not found.
Did you mean:
- program
> xyz
✗ 'xyz' not found.
No suggestions found. Add to dictionary? (y/n): y
'xyz' added to dictionary!
> exit
Goodbye!┌─────────────┐
│ Enter Word │
└──────┬──────┘
│
▼
┌─────────────────┐
│ Exact Match? │
└────┬────────┬───┘
│ YES │ NO
▼ ▼
✓ Correct ┌──────────────┐
│ Prefix Match? │
└───┬──────┬────┘
│ YES │ NO
▼ ▼
Auto-Complete ┌─────────────┐
│ Suggestions │
└──────┬──────┘
│
▼
┌──────────────┐
│ Add to Dict? │
└──────────────┘
File: dictionary.txt (700+ common English words)
about
above
across
action
...
young
your
yourself
Format Rules:
- One word per line
- Lowercase only
- No punctuation or spaces
- UTF-8 encoding
Dataset: 700+ most common English words for optimal coverage
Spell Checker/
├── 📄 SpellCheckerUsingTrie.cpp # Main implementation (300+ lines)
├── 📊 dictionary.txt # 700+ common English words
├── 📖 Readme.md # This file
└── 🔧 SpellCheckerUsingTrie.exe # Compiled executable
| Component | Responsibility |
|---|---|
| TrieNode | Node structure with end-of-word flag and children map |
| Trie | Core class with insert, search, delete, auto-complete |
| getSuggestions() | Generates all 1-edit distance candidates |
| autoComplete() | DFS traversal for prefix matching |
| populateFromDictionary() | Loads words from file into trie |
| main() | Interactive CLI loop with user input handling |
✅ C++11 compatible compiler (g++, Clang, MSVC)
✅ Windows.h (for UTF-8 console output on Windows)# Compile
g++ -o spell_checker SpellCheckerUsingTrie.cpp -std=c++11
# Run
./spell_checker # Linux/macOS
spell_checker.exe # Windows| Input Type | Behavior | Example |
|---|---|---|
| Complete Word | Spell check | hello → ✓ Correct |
| Prefix | Auto-complete | prog → program, progress |
| Misspelled | Suggestions | helo → hello, help |
| Unknown | Add to dictionary | xyz → Add? (y/n) |
| Exit | Quit program | exit → Goodbye! |
Trie Data Structure • Prefix Trees • DFS Traversal
Edit Distance • String Algorithms • Hash Maps
File I/O • Memory Management • Recursive Algorithms
| Concept | Implementation |
|---|---|
| Trie Construction | Insert words character-by-character with shared prefixes |
| Prefix Matching | Navigate to prefix node, then DFS for all completions |
| Edit Distance | Generate all 1-edit candidates (4 operations × word length) |
| Memory Management | Recursive destructor to free all nodes |
| Hash Map Usage | unordered_map<char, TrieNode*> for O(1) child lookup |
- Frequency Ranking — Sort suggestions by word popularity
- Multi-Language — Support for different language dictionaries
- Context-Aware — Consider surrounding words for better suggestions
- Phonetic Matching — Soundex/Metaphone for sound-alike words
- Edit Distance 2 — Expand to 2-character edits for better coverage
- Fuzzy Matching — Levenshtein distance for similarity scoring
Dictionary Size: 700+ words | Trie Depth: ~15 levels
Avg Suggestions: 3-5 per misspelling | Lookup Time: <1ms
Part of the DSA Projects Roadmap — Phase 2, Project #9
✏️ Write with Confidence! 📝