forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(CPlusPlus): add find words matching pattern dictionary (MakeCon…
…tributions#1050) * Create find_all_words_matching_pattern_in_given_dictionary.cpp Given a dictionary of words where each word follows a CamelCase notation, find all words in it that matches a given pattern of all uppercase characters. We can use a Trie data structure to solve this problem. The idea is to insert all uppercase characters of each word in the CamelCase dictionary into a Trie. Expected output: HiTech HiTechLab HiTechCity * Update README.md
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
algorithms/CPlusPlus/Trie/find_all_words_matching_pattern_in_given_dictionary.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
#include <string> | ||
using namespace std; | ||
|
||
// Data structure to store a Trie node | ||
struct TrieNode | ||
{ | ||
// each node stores a map to its child nodes | ||
unordered_map<char, TrieNode*> map; | ||
|
||
// true when the node is a leaf node | ||
bool isLeaf = false; | ||
|
||
// collection to store a complete list of words in the leaf node | ||
unordered_set<string> word; | ||
}; | ||
|
||
// Function to insert a string into a Trie | ||
void insert(TrieNode*& head, string word) | ||
{ | ||
if (head == nullptr) { | ||
head = new TrieNode(); | ||
} | ||
|
||
// start from the head node | ||
TrieNode* curr = head; | ||
for (char c: word) | ||
{ | ||
// insert only uppercase characters | ||
if (isupper(c)) | ||
{ | ||
// create a new node if the path doesn't exist | ||
if (curr->map.find(c) == curr->map.end()) { | ||
curr->map[c] = new TrieNode(); | ||
} | ||
|
||
// go to the next node | ||
curr = curr->map[c]; | ||
} | ||
} | ||
|
||
// mark the current node as a leaf | ||
curr->isLeaf = true; | ||
|
||
// push the current word into the set associated with a leaf node | ||
(curr->word).insert(word); | ||
} | ||
|
||
// Function to print all children of a given Trie node | ||
void printAllWords(TrieNode* root) | ||
{ | ||
// if the current node is a leaf, print all words associated with it | ||
if (root->isLeaf) | ||
{ | ||
unordered_set<string> collection = root->word; | ||
for (string s: collection) { | ||
cout << s << endl; | ||
} | ||
} | ||
|
||
// recur for all children of the root node | ||
for (auto pair: root->map) | ||
{ | ||
TrieNode* child = pair.second; | ||
if (child) { | ||
printAllWords(child); | ||
} | ||
} | ||
} | ||
|
||
// Function to print all words in the CamelCase dictionary, which | ||
// matches the given pattern | ||
void findAllWords(vector<string> const &dictionary, string pattern) | ||
{ | ||
// base case | ||
if (dictionary.size() == 0) { | ||
return; | ||
} | ||
|
||
// Trie head node | ||
TrieNode* head = nullptr; | ||
|
||
// construct a Trie from the given dictionary | ||
for (string s: dictionary) { | ||
insert(head, s); | ||
} | ||
|
||
// search for the given pattern in the Trie | ||
TrieNode* curr = head; | ||
for (char c: pattern) | ||
{ | ||
// move to the child node | ||
curr = curr->map[c]; | ||
|
||
// if the given pattern is not found (reached end of a path in the Trie) | ||
if (curr == nullptr) { | ||
return; | ||
} | ||
} | ||
|
||
// print all words matching the given pattern | ||
printAllWords(curr); | ||
} | ||
|
||
int main() | ||
{ | ||
vector<string> dictionary { | ||
"Hi", "HiTech", "HiTechCity", "Techie", "TechieDelight", | ||
"Hello", "HelloWorld", "HiTechLab" | ||
}; | ||
|
||
string pattern = "HT"; | ||
|
||
findAllWords(dictionary, pattern); | ||
|
||
return 0; | ||
} |