-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathPatternMatching.cpp
102 lines (86 loc) · 1.89 KB
/
PatternMatching.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// #include "TrieNode.h"
#include <string>
#include <vector>
class TrieNode {
public :
char data;
TrieNode **children;
bool isTerminal;
TrieNode(char data) {
this -> data = data;
children = new TrieNode*[26];
for(int i = 0; i < 26; i++) {
children[i] = NULL;
}
isTerminal = false;
}
};
class Trie {
TrieNode *root;
public :
int count;
Trie() {
this->count = 0;
root = new TrieNode('\0');
}
bool insertWord(TrieNode *root, string word) {
// Base case
if(word.size() == 0) {
return root -> isTerminal;
}
// Small Calculation
int index = word[0] - 'a';
TrieNode *child;
if(root -> children[index] != NULL) {
child = root -> children[index];
}
else {
child = new TrieNode(word[0]);
root -> children[index] = child;
}
// Recursive call
return insertWord(child, word.substr(1));
}
// For user
void insertWord(string word) {
if (insertWord(root, word)) {
this->count++;
}
}
bool search(TrieNode *root, string word)
{
// base condition
if(word.size()==0){
return true;
}
//small calculation
int index=word[0]-'a';
TrieNode *child;
if( root -> children[index]!=NULL)
{
child=root->children[index];
return search(child,word.substr(1));
}
else
{
return false;
}
}
bool patternMatching(vector<string> vect, string pattern) {
// Complete this function
// Return true or false
// cout<<vect.size()<<endl;
for(int i=0;i<vect.size();i++)
{
//cout<<vect[i]<<endl;
string word=vect[i];
while(word.size()!=0)
{
// cout<<word<<endl;
insertWord(word);
word=word.substr(1);
}
}
return search(root,pattern);
}
};