Given a sorted dictionary of an alien language having N-words and k starting alphabets of a standard dictionary. Find the order of characters in the alien language.
Note: Many orders may be possible for a particular test case, thus you may return any valid order.
Note: Many orders may be possible for a particular test case, thus you may return any valid order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N, and the integer K as inputs and returns a string denoting the order of characters in the alien language.
You don't need to read input or print anything. Your task is to complete the function findOrder() which takes the string array dict[], its size N, and the integer K as inputs and returns a string denoting the order of characters in the alien language.
// { Driver Code Starts
// Initial Template for C++
/* script for test case http://code.geeksforgeeks.org/PetjYq */
#include <bits/stdc++.h>
using namespace std;
string findOrder(string[], int, int);
string order;
bool f(string a, string b) {
int p1 = 0;
int p2 = 0;
for (int i = 0; i < min(a.size(), b.size()) and p1 == p2; i++) {
p1 = order.find(a[i]);
p2 = order.find(b[i]);
// cout<<p1<<" "<<p2<<endl;
}
if (p1 == p2 and a.size() != b.size()) return a.size() < b.size();
return p1 < p2;
}
// Driver program to test above functions
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
string s[n];
for (int i = 0; i < n; i++) cin >> s[i];
string ss = findOrder(s, n, k);
order = "";
for (int i = 0; i < ss.size(); i++) order += ss[i];
string temp[n];
std::copy(s, s + n, temp);
sort(temp, temp + n, f);
bool f = true;
for (int i = 0; i < n; i++)
if (s[i] != temp[i]) f = false;
cout << f;
cout << endl;
}
return 0;
}
// } Driver Code Ends
// User function Template for C++
/*
dict : an array of strings denoting the words in alien langauge
N : Size of the dictionary
K : Number of characters
*/
// string findOrder(string dict[], int N, int K) {
// // Your code here
// string s,st,ans="";
// // vector<char> v;
// if(N==1)
// return dict[0];
// s=dict[0];
// st=dict[1];
// for(int i=0;i<min(s.size(),st.size());++i)
// {
// if(s[i]!=st[i])
// {
// ans.push_back(s[i]);
// ans.push_back(st[i]);
// break;
// }
// }
// int k=1;
// while(k<(N-2))
// {
// s=dict[k];
// st=dict[k+1];
// for(int i=0;i<min(s.size(),st.size());++i)
// {
// if(s[i]!=st[i])
// {
// string h;
// size_t found=ans.find(s[i]);
// size_t f=ans.find(st[i]);
// if((found!=string::npos)&&(f!=string::npos))
// {
// continue;
// }
// else if(found!=string::npos)
// {
// h+=st[i];
// ans.insert(found+1,h);
// }else{
// h+=s[i];
// ans.insert(f,h);
// }
// break;
// }
// }
// ++k;
// }
// if(ans.size()<=K)
// return ans;
// else
// return "";
// }
stack<int> st;
void topologicals(vector<int> v[],vector<int> & visited,int j)
{
if(visited[j])
return;
visited[j]=1;
for(int i=0;i<v[j].size();++i)
{
if(!visited[v[j][i]])
topologicals(v,visited,v[j][i]);
}
st.push(j);
}
string findOrder(string dict[], int N, int K) {
vector<int> *v=new
vector<int> [K];
for(int i=0;i<N-1;++i)
{
string s=dict[i],sl=dict[i+1];
int j=0;
while(j<min(s.size(),sl.size()))
{
if(s[j]!=sl[j])
{
v[s[j]-'a'].push_back(sl[j]-'a');
break;
}
++j;
}
}
vector<int> visited(26,0);
for(int i=0;i<K;++i)
{
if(!visited[i])
topologicals(v,visited,i);
}
string ans;
while(!st.empty())
{
ans.push_back(st.top()+'a');
st.pop();
}
// cout<<ans<<endl;
return ans;
}