-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordladder2.cpp
More file actions
115 lines (107 loc) · 2.81 KB
/
Copy pathwordladder2.cpp
File metadata and controls
115 lines (107 loc) · 2.81 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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution
{
public:
vector<vector<string>> findSequences(string beginWord, string endWord, vector<string> &wordlist)
{
// code here
queue<vector<string>> q;
q.push({beginWord});
unordered_set<string> st(wordlist.begin(), wordlist.end());
vector<string> usedOnLevel;
usedOnLevel.push_back(beginWord);
int level = 0;
vector<vector<string>> res;
while (!q.empty())
{
vector<string> v = q.front();
// erase all words that have been used
// at previous level to transform
if (v.size() > level)
{
level++;
for (auto it : usedOnLevel)
{
st.erase(it);
}
}
string word = v.back();
q.pop();
if (word == endWord)
{
if (res.size() == 0)
{
res.push_back(v);
}
else if (v.size() == res[0].size())
{
res.push_back(v);
}
}
for (int i = 0; i < word.size(); i++)
{
char original = word[i];
for (char ch = 'a'; ch <= 'z'; ch++)
{
word[i] = ch;
if (st.find(word) != st.end())
{
v.push_back(word);
usedOnLevel.push_back(word);
q.push(v);
v.pop_back();
}
}
word[i] = original;
}
}
return res;
}
};
//{ Driver Code Starts.
bool comp(vector<string> a, vector<string> b)
{
string x = "", y = "";
for (string i : a)
x += i;
for (string i : b)
y += i;
return x < y;
}
int main()
{
int tc;
cin >> tc;
while (tc--)
{
int n;
cin >> n;
vector<string> wordList(n);
for (int i = 0; i < n; i++)
cin >> wordList[i];
string startWord, targetWord;
cin >> startWord >> targetWord;
Solution obj;
vector<vector<string>> ans = obj.findSequences(startWord, targetWord, wordList);
if (ans.size() == 0)
cout << -1 << endl;
else
{
sort(ans.begin(), ans.end(), comp);
for (int i = 0; i < ans.size(); i++)
{
for (int j = 0; j < ans[i].size(); j++)
{
cout << ans[i][j] << " ";
}
cout << endl;
}
}
}
return 0;
}
// } Driver Code Ends