-
Notifications
You must be signed in to change notification settings - Fork 8
/
google_find-string.cpp
executable file
·152 lines (141 loc) · 3.73 KB
/
google_find-string.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Given a string and array of strings, find whether the array contains a string with one character difference
from the given string. Array may contain string of different lengths.
Ex: Given string
banana
and array is
[bana, apple, banaba, bonanza, banamf]
and the outpost should be true as banana and banaba are one character difference.
*/
#include <bits/stdc++.h>
using namespace std;
struct Trienode{
bool isleaf;
Trienode *child[26];
};
Trienode *getnewnode(){
Trienode *newnode = new Trienode();
newnode->isleaf = false;
for(int i=0;i<26;i++){
newnode->child[i] = NULL;
}
return newnode;
}
void build(Trienode *root, string s){
int len = s.length();
Trienode *crawl = root;
for(int i=0;i<len;i++){
int index = s[i] - 97;
if(!crawl->child[index]){
crawl->child[index] = getnewnode();
}
crawl = crawl->child[index];
}
crawl->isleaf = true;
}
int search(Trienode *root, string s){
Trienode *crawl = root;
Trienode *prev = NULL;
int len = s.length();
int flag = 0;
int i = 0, j = 0;
int start = 0;
for(i=0;i<len;i++){
int index = s[i] - 97;
if(!crawl->child[index] && flag == 0){
flag = 1;
}else if(!crawl->child[index] && flag == 1){
return 0;
}
if(flag == 1){
start = i;
int flag2 = 0;
//check if there exists a word of len = start + 1;
if(start + 1 == len){
Trienode *temp = crawl;
for(int k=0;k<26;k++){
if(temp->child[k]){
temp = temp->child[k];
if(temp->isleaf == 1){
flag2 = 1;
return 1;
}
}
}
if(flag2 == 0) return 0;
}
break;
}
crawl = crawl->child[index];
}
int check = 0;
if(flag == 1){
for(j=0;j<26;j++){
if(crawl->child[j]){
prev = crawl;
check = 1;
crawl = crawl->child[j];
for(i = start+1;i<len;i++){
int index = s[i] - 97;
if(!crawl->child[index]){
break;
}
crawl = crawl->child[index];
}
}
if(i == len && crawl->isleaf == 1){
return 1;
}else if(check == 1){
crawl = prev;
}
}
if(j == 26) return 0;
}
return 0;
}
int main(){
vector<string> arr;
arr.push_back("bana");
arr.push_back("apple");
arr.push_back("banaba");
arr.push_back("bonanza");
arr.push_back("banamf");
Trienode *root = getnewnode();
for(int i=0;i<arr.size();i++){
build(root,arr[i]);
}
vector<string> testarr;
testarr.push_back("banana");
testarr.push_back("bannna");
testarr.push_back("appl");
testarr.push_back("abcd");
for(int i=0;i<testarr.size();i++){
int res = search(root,testarr[i]);
(res == 1)?(cout<<"Yes\n"):(cout<<"No\n");
}
return 0;
}
/*
struct Node {
bool exist;
Node *children[256];
};
Node *root;
bool dfs(Node *n, const string &s, int pos, int cnt) {
if (cnt > 1)
return false;
if (pos == s.size())
return cnt == 1;
for (int i = 0; i < 256; ++i) {
Node *c = n->children[i];
if (c != nullptr) {
if (s[pos] == i && dfs(c, s, pos + 1, cnt) ||
s[pos] != i && dfs(c, s, pos + 1, cnt + 1))
return true;
}
}
}
bool hasSimilar(const string &s) {
return dfs(root, s, 0, 0);
}
*/