-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-577.cpp
47 lines (47 loc) · 1.27 KB
/
Day-577.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
map<char , vector<string>>fr;
map<string , vector<string>>graph;
unordered_set<string>visited;
void dfs(string&node) {
if(visited.find(node) != visited.end()) {
return ;
}
visited.insert(node);
for(auto&itr:graph[node]) {
dfs(itr);
}
}
public:
bool chainPresent(vector<string>&v) {
fr.clear();
visited.clear();
bck.clear();
graph.clear();
for(auto&itr:v) {
fr[itr[0]].push_back(itr);
}
for(auto&itr:fr) {
for(auto&it:itr.second) {
char lastchar = it[it.length()-1];
for(auto&i:fr[lastchar]) {
graph[it].push_back(i);
}
}
}
dfs(v[0]);
return visited.size() == v.size();
}
};
signed main(void){
vector<string>v = {"chair", "height", "racket","touch", "tunic"};
//vector<string>v = {"aa" , "ba" , "ab" };
Solution s;
if (s.chainPresent(v)) {
cout << "Can be Chained" << '\n';
} else {
cout << "Can't chained" << '\n';
}
return 0;
}