- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Alien Dictionary in C++
Suppose there is a new alien language and that uses the latin alphabet. However, the order among letters are not known. We have a list of non-empty words from the dictionary, these words are sorted lexicographically by the rules of this new language. we have to find the order of letters in this language.
So, if the input is like ["wrt","wrf","er", "ett", "rftt" ], then the output will be "wertf"
To solve this, we will follow these steps −
Define one map called degree
Define one map called graph
n := size of words
for initialize i := 0, when i < size of words, update (increase i by 1), do −
for initialize j := 0, when j < size of words[i], update (increase j by 1), do −
degree[words[i, j]] := 0
for initialize i := 0, when i < n - 1, update (increase i by 1), do −
l := minimum of size of words[i] and size of words[i + 1]
for initialize j := 0, when j < l, update (increase j by 1), do −
x := words[i, j]
y := words[i + 1, j]
if x is not equal to y, then −
insert y at the end of graph[x]
(increase degree[y] by 1)
Come out from the loop
ret := blank string
Define one queue q
For each key-value pair it in degree, do −
if value of it is same as 0, then −
insert key of it into q
while (not q is empty), do −
x := first element of q
delete element from q
ret := ret + x
for each element sit in graph do −
decrease degree[sit] by 1
if degree[sit] is same as 0, then −
insert sit into q
(increase sit by 1)
return (if size of ret is same as size of degree, then ret, otherwise blank string)
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: string alienOrder(vector<string>& words) { map<char, int> degree; map<char, vector<char> > graph; int n = words.size(); for (int i = 0; i < words.size(); i++) { for (int j = 0; j < words[i].size(); j++) { degree[words[i][j]] = 0; } } for (int i = 0; i < n - 1; i++) { int l = min((int)words[i].size(), (int)words[i + 1].size()); for (int j = 0; j < l; j++) { char x = words[i][j]; char y = words[i + 1][j]; if (x != y) { graph[x].push_back(y); degree[y]++; break; } } } string ret = ""; queue<char> q; map<char, int>::iterator it = degree.begin(); while (it != degree.end()) { if (it->second == 0) { q.push(it->first); } it++; } while (!q.empty()) { char x = q.front(); q.pop(); ret += x; vector<char>::iterator sit = graph[x].begin(); while (sit != graph[x].end()) { degree[*sit]--; if (degree[*sit] == 0) { q.push(*sit); } sit++; } } return ret.size() == degree.size() ? ret : ""; } }; main(){ Solution ob; vector<string> v = {"wrt","wrf","er","ett","rftt"}; cout <<(ob.alienOrder(v)); }
Input
{"wrt","wrf","er","ett","rftt"}
Output
wertf