-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWL.cpp
More file actions
142 lines (115 loc) · 3.37 KB
/
WL.cpp
File metadata and controls
142 lines (115 loc) · 3.37 KB
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
#include<bits/stdc++.h>
using namespace std;
#include "leetcodeHelper.h"
class Solution {
public:
class Trie {
public:
unordered_map<char, Trie*> children;
// For getting end of word
bool endOfWrord;
char ch;
Trie(char ch) {
this->ch = ch;
endOfWrord = false;
children[ch]=NULL;
}
};
void insert(Trie *root , string &word , int i = 0) {
if (i == word.size()) {
// Set end of word as true and return
root->endOfWrord = true;
return;
}
// If already an subtree then use it else make a new subtree node
if (!root->children[word[i]]) {
root->children[word[i]] = new Trie(word[i]);
}
// insert rest part of the word into tries
insert(root->children[word[i]] , word , i + 1);
}
void getAllPossible(Trie *root , string &str , vector<string> &allPossible ,int &c, string curr="" , bool changed = false,unsigned int i=0) {
// We can not get any string
if (!root) return;
// Finaly we got a string but will store only when it is a valid word from wordList
if (curr.size() == str.size()) {
if(root->endOfWrord)
allPossible.push_back(curr);
return;
}
// Explore all the node
if(changed){
if(root -> children.find(str[i]) != root -> children.end()){
curr.push_back(str[i]);
Trie* node = root -> children[str[i]];
getAllPossible(node , str , allPossible , c, curr , changed , i + 1);
}
}
else{
for (auto x : root->children) {
c++;
curr.push_back(x.first);
if (x.first == str[i] ) {
// We are not replacing any char
getAllPossible(x.second , str , allPossible , c, curr , changed , i + 1);
} else {
// Replace a character only when if it has not yet changed single time
getAllPossible(x.second , str , allPossible ,c, curr , !changed , i + 1);
}
// BackTrack what we stored
curr.pop_back();
}
}
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
// Make a Trie
Trie *root = new Trie('\0');
// Insert all word from wordList into trie
for (auto x : wordList) {
insert(root , x);
}
// For Breadth First Search
queue<string> q;
// Push beginWord into queue
q.push(beginWord);
// For mark whether any string already visited or not?
unordered_map<string,int> visited;
// Begin Word at level 1
visited[beginWord]=1;
// Iterate over queue until it does not become empty
int loop = 0;
while (q.size()>0) {
// Take the front from queue
string str = q.front();
q.pop();
loop++;
// If str is endWord then return it
if(str==endWord) {
cout<<loop<<endl;
return visited[str];
}
// Explore all possibilities which can be make way replacing a single character
int c = 0;
vector<string> allPossible;
getAllPossible(root , str , allPossible, c);
cout<<c<<" iterations for word "<<str<<endl;
// Push into queue if they are not visited yet!!!
for(auto x:allPossible){
if(visited.count(x)==0){
q.push(x);
visited[x]=visited[str]+1;
}
}
}
return 0;
}
};
int main(){
Solution s;
string begin, end, arr;
cin>> begin >> end >> arr;
begin = begin.substr(1, begin.size()-2);
end = end.substr(1, end.size()-2);
vector<string> words = split(arr, ',');
cout<<s.ladderLength(begin, end, words)<<endl;
}