chore(CPlusPlus): add find words matching pattern dictionary (#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
pull/1093/head
Yashkumar Gupta 2022-11-30 18:15:11 +05:30 committed by GitHub
parent 7aa0b7be6f
commit 4b2835bae6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 0 deletions

View File

@ -160,6 +160,7 @@
- [Trie for searching](Trie/trie_search.cpp)
- [Trie for insert search and prefix_search](Trie/trie_insert_search_startWith.cpp)
- [Trie for delete](Trie/trie_delete.cpp)
- [Find Words Matching Pattern Dictionary](Trie/find_all_words_matching_pattern_in_given_dictionary.cpp)
# Maths

View File

@ -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;
}