-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathword_lad_2.cpp
65 lines (54 loc) · 1.85 KB
/
word_lad_2.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
/*
GFG link: https://practice.geeksforgeeks.org/problems/word-ladder-ii/1
Video: https://youtu.be/DREutrv2XD0
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<string>> findSequences(string beginWord, string endWord, vector<string>& wordList) {
// code here
unordered_set<string> st(wordList.begin(), wordList.end());
queue<vector<string>> q;
q.push({beginWord});
// no of list of strings in queue belonging to same level
vector<string> usedOnLevel;
usedOnLevel.push_back(beginWord);
int level = 0;
vector<vector<string>> ans;
while(!q.empty())
{
vector<string> curr = q.front();
q.pop();
// erase all word that have been
// used in the prev levels to transform
if(curr.size() > level)
{
++level;
for(auto it: usedOnLevel)
st.erase(it);
usedOnLevel.clear();
}
string word = curr.back();
if(word == endWord)
ans.push_back(curr);
for(int i = 0; i < word.size(); i++) // h,a,t
{
char og = word[i];
for(char it = 'a'; it <= 'z'; it++) // ha, hb, hz..aa,ab,ac..
{
word[i] = it;
if(st.find(word) != st.end())
{
curr.push_back(word);
q.push(curr);
usedOnLevel.push_back(word);
curr.pop_back();
}
}
word[i] = og;
}
}
return ans;
}
};