-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-022.cpp
115 lines (109 loc) · 3.09 KB
/
Day-022.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
/*
*
* This problem was asked by Microsoft.
Given a dictionary of words and a string made up of those words (no spaces),
return the original sentence in a list.
If there is more than one possible reconstruction, return any of them.
If there is no possible reconstruction, then return null.
For example, given the set of words 'quick', 'brown', 'the', 'fox', and
the string "thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox'].
Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and
the string "bedbathandbeyond", return either ['bed', 'bath', 'and', 'beyond] or ['bedbath', 'and', 'beyond'].
*
*
*
*/
/*
*
* Idea is use Trie and store the words in our tree and then in the
* given string for each character we search in the tree if not found then
* answer can't be constructed , we will handle it by making the okay [bool variable ] false
*
* otherwise we will get the answer in the end and print that ;
*
*
*/
#include <bits/stdc++.h>
#define ALPHABETS 26
using namespace std;
struct Node{
Node*pointer[ALPHABETS];
bool is_End_Of_Word;
Node(){
for(int i=0;i<ALPHABETS;++i){
pointer[i]=nullptr;
}
is_End_Of_Word=false;
}
};
void add(Node*Root , string word){
int word_Size = (int)word.size();
Node*root = Root;
for(int i=0;i<word_Size;++i){
int index = word[i]-'a';
if(root->pointer[index]==nullptr){
root->pointer[index] = new Node();
}
root = root->pointer[index];
}
root->is_End_Of_Word=true;
}
bool okay = true; // just for the test whether answer exist or not
string getans(Node*Root,string &test_String , int index_Traversed=0){
if(!okay){
return "";
}
Node*root = Root;
string temp="";
for(int &i=index_Traversed;okay && i<test_String.size();++i){
int index = test_String[i]-'a';
if(root->pointer[index]==nullptr){
okay = false;
}else{
temp+=char(int('a')+index);
root=root->pointer[index];
}
if(root->is_End_Of_Word){
index_Traversed++;
break;
}
}
if(index_Traversed>=(int)test_String.size()){
return temp;
}
temp+=" ";
if(!okay){
return "";
}else{
return temp+getans(Root,test_String,index_Traversed);
}
}
int main(void){
Node*root1 = new Node();
add(root1,"quick");
add(root1,"brown");
add(root1,"the");
add(root1,"fox");
string test1="thequickbrownfox";
string res1 = getans(root1,test1);
if(okay){
cout<<res1<<endl;
}else{
cout<<"NO RESULT"<<endl;
}
okay = true; // for the next test let's just make it true again [might possible that last test make it false]
Node*root2 = new Node();
add(root2,"bed");
add(root2,"bath");
add(root2,"bedbath");
add(root2,"and");
add(root2,"beyond");
string test2 = "bedbathandbeyond";
string res2 = getans(root2,test2);
if(okay){
cout<<res2<<endl;
}else{
cout<<"NO RESULT"<<endl;
}
return 0;
}