-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInterViewSet1.cpp
156 lines (119 loc) · 4.06 KB
/
InterViewSet1.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
153
154
155
156
{"house", "cat", "hope", "hair"}
"ho" -> "house", "hope"
"cat" -> "cat"
"h" -> "house", "hope", "hair"
struct trie {
trie *map<char,trie> child ;
bool isLeaf;
trie() {
for(auto char : child) {
child[char] = NULL;
}
isLeaf = false;
}
}
void insert(trie * root,string &s, int st){
for(int i = st ; i < s.size(); i++ ) {
if(root->child[s[i]] == NULL) root->child[s[i]] = new trie();
root = root->child[s[i]];
}
root->isLeaf = true;
}
bool search(trie * root, string & word, int st) {
for(int i = 0 ; i < word.size(); i ++) {
if(root->child[word[i]] == NULL) return false;
root = root->child[word[i]];
}
return true;
}
int main() {
int n;
cin>>n;
vector<string> BusinessName;
for(int )// input
string searchTerm;
for(auto name : BusinessName) {
trie * root = new trie();
insert(name,root,0);
for(int i = 1; i +1 < name.size(); i++ ) {
if(name[i]== ' '){
insert(root,name,i+1);
}
}
}
if(search(root,searchTerm)){
cout<<name<<endl;
}
}
----------------------------------
/*
Q1. HeadToTail
In this puzzle game, sometimes called Word Ladder, the goal is to find a path of words between two English words with two simple rules:
- You can change only one letter at a time
- Every word in the steps has to be a valid English word.
Here is an example, going from "HEAD" to "TAIL" (make sure to emphasize which letter is changed on each step):
- HEAD
- HEAL
- TEAL
- TELL
- TALL
- TAIL
bool EnglishDictionary.isValidWord(String word);
*/
static class Pair<T,U> {
T left;
U right;
public Pair(T left,U right){
this.left = left;
this.right = right;
}
public <T,U> T getLeft(){
return (T) this.left;
}
public <T,U> T getRight(){
return (T) this.right;
}
}
class Node {
String CurrWord;
List<String> path = new ArrayList();
public Node(string CurrWord,List<String> prevPath) {
path.addAll(prevPath);
path.add(currWord);
this.CurrWord = CurrWord;
}
private List<Pair<String,Integer>> getNeighbors(Set<string> visited,Node node){
String cur = node.currWord;
List<Pair<String,Integer>> neighbors = new ArrayList<>();
for(int i = cur.size; i > 0; i --) {
for(int j = 0; j< 26;j ++ ){
String temp = cur;
if (cur[i]!='A'+j) {
temp[i]='A'+j;
if(isValidWord(temp)) {
//add to neighbor
neigbors.add(new Pair<>(temp,i));
}
}
}
}
return neighbors;
}
public List<String> getPath(Set<String> dev, String beginword,String endWord) {
Queue<Node> q = new LinkedList<>();
List<String> path = new ArrayList<>();
q.add(new Node(beginword,path));
Set<String> visited = new HashSet<>();
visited.add(beginword);
while(!q.isEmpty()) {
Node curr = q.poll();
if(curr.CurrWord.equals(endWord)){
System.out.println(curr.path);
return curr.path;
}
for(Pair<String,Integer> neighbour : getNeighbour(visited,curr)) {
q.add(new Node
}
}
}
}