Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution{
- public:
- vector<int> adjlst[30];
- int vis[30];
- string ans="";
- stack<int> S;
- void toplogicalSort(int v){
- if(vis[v]==1) return ;
- vis[v]=1;
- for(auto i:adjlst[v]){
- toplogicalSort(i);
- }
- S.push(v);
- }
- string findOrder(string dict[], int N, int K) {
- for(int i=1;i<N;i++){
- int j=0;
- int z=min(dict[i-1].length(),dict[i].length());
- while(j<z and dict[i-1][j]==dict[i][j]) j++;
- if(j==z) continue;
- else{
- adjlst[dict[i-1][j]-'a'].push_back(dict[i][j]-'a');
- }
- }
- for(int i=0;i<26;i++){
- toplogicalSort(i);
- }
- while(!S.empty()){
- char c=S.top()+'a';
- ans+=c;
- S.pop();
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment