-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathAlien Dictionary.cpp
100 lines (83 loc) Β· 3.04 KB
/
Alien Dictionary.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
/*
TC & Space: O(N*M) where N is no. of words & M is average length of each word
1. First, build a degree map for each character in all the words:
2. Then build the hashmap by comparing the adjacent words, the first character that is different between two adjacent words reflect the lexicographical order.
3. Then in last call a topological sorting() to get the required ordering of alphabets in dictionary
*/
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
string alienOrder(vector<string>& words)
{
unordered_map<char, unordered_set<char>> graph;
vector<int> indegree(26);
buildGraph(words, graph, indegree);
return bfs(graph, indegree); //topological sorting using BFS to get the required ordering
}
private:
void buildGraph(vector<string>& words, unordered_map<char, unordered_set<char>> &graph, vector<int> &indegree)
{
for(string word: words) // add all charcters/alphabet to graph
for(char c: word)
graph[c]={};
for(int i=0; i<words.size()-1; i++)
{
string word1=words[i], word2=words[i+1];
int j;
for(j=0; j<min(word1.length(), word2.length()); j++)
{
if(word1[j] != word2[j]) // add an edge from u->v if u comes before v in 2 adjacent words
{
char out = word1[j], in=word2[j];
graph[out].insert(in);
indegree[in-'a']++;
break;
}
}
if(j==min(word1.length(), word2.length()) && word1.length()>word2.length()) //consider case [apple, app] i.e when ordering isn't valid
{
graph.clear();
return;
}
}
}
string bfs(unordered_map<char, unordered_set<char>> &graph, vector<int> &indegree)
{
string res="";
int totalCharacters=graph.size();
queue<char> q;
for(auto it: graph) //enque all the vertices with 0 indegree becz they don't have any dependency to met, their precedence is greater in dictionary
if(indegree[it.first-'a']==0)
{
res += it.first;
q.push(it.first);
}
while (!q.empty()) //standard BFS algorithm
{
char curr = q.front();
q.pop();
if(graph[curr].size() != 0) //if current nodes has neighbours
{
for(char neigh: graph[curr])
{
indegree[neigh-'a']--;
if(indegree[neigh-'a'] == 0)
{
q.push(neigh);
res += neigh;
}
}
}
}
return (res.length()==totalCharacters?res:"");
}
};
int main()
{
Solution obj;
vector<string> words={"wrt","wrf","er","ett","rftt"};
string output = "wertf"
cout<<obj.alienOrder(words);
return 0;
}