Advertisement
i_love_rao_khushboo

Untitled

Aug 11th, 2022
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. /* PROBLEM STATEMENT: There is a dictionary containing words from an alien language for which we don’t
  2.                       know the ordering of the characters. Write a method to find the correct order of
  3.                       characters in the alien language.
  4.                       NOTE: The words are sorted lexicographically by the rules of the alien language
  5.                             as it is given that a dictionary is containing the words.
  6.                       Input: Words: ["cab", "aaa", "aab"]
  7.                       Output: cab
  8.                       Explanation: From the given words we can conclude the following ordering among
  9.                       its characters:
  10.                          
  11.                       1. From "cab" and "aaa", we can conclude that 'c' comes before 'a'.
  12.                       2. From "aaa" and "aab", we can conclude that 'a' comes before 'b'
  13.                          
  14.                       From the above two points, we can conclude that the correct character order is: "cab"
  15. */
  16.  
  17. /* UNDERLYING CONCEPT ------>
  18.    # Since the given words are sorted lexicographically by the rules of the alien language, we can
  19.      always compare two adjacent words to determine the ordering of the characters.
  20.    1. Take the first two words for eg. “ba” and “bc”. Starting from the beginning of the words, find the first
  21.      character that is different in both words: it would be ‘a’ from “ba” and ‘c’ from “bc”. Because of
  22.      the sorted order of words (i.e. the dictionary!), we can conclude that ‘a’ comes before ‘c’ in the
  23.      alien language.
  24.    2. Similarly, from “bc” and “ac”, we can conclude that ‘b’ comes before ‘a’.
  25.    # These two points tell us that we are actually asked to find the topological ordering of the characters,
  26.      and that the ordering rules should be inferred from adjacent words from the alien dictionary.
  27.  
  28.    # This makes the current problem similar to Tasks Scheduling Order, the only difference being that we need
  29.      to build the graph of the characters by comparing adjacent words first, and then perform the topological
  30.      sort for the graph to determine the order of the characters.
  31. */
  32.  
  33. #include<iostream>
  34. #include<vector>
  35. #include<unordered_map>
  36. #include<queue>
  37. using namespace std;
  38.  
  39. class TopologicalSort
  40. {
  41.     public:
  42.         static string sort(const vector<string> &v)
  43.         {
  44.             if(v.size()==0)
  45.                 return "";
  46.                
  47.             string res;
  48.             unordered_map<char, int> inDegree;
  49.             unordered_map<char, vector<char>> graph;
  50.            
  51.             for(auto word: v)
  52.             {
  53.                 for(auto ch: word)
  54.                 {
  55.                     inDegree[ch]=0;
  56.                     graph[ch]=vector<char>();
  57.                 }
  58.             }
  59.            
  60.             for(int i=0; i<v.size()-1; i++)
  61.             {
  62.                 string s1=v[i], s2=v[i+1];
  63.                 for(int j=0; j<min(s1.length(), s2.length()); j++)
  64.                 {
  65.                     char parent=s1[j], child=s2[j];
  66.                     if(parent!=child)
  67.                     {
  68.                         graph[parent].push_back(child);
  69.                         inDegree[child]++;
  70.                         break;
  71.                     }
  72.                 }
  73.             }
  74.            
  75.             queue<char> q;
  76.             for(auto entry: inDegree)
  77.             {
  78.                 if(entry.second==0)
  79.                     q.push(entry.first);
  80.             }
  81.            
  82.             while(!q.empty())
  83.             {
  84.                 char vertex=q.front();
  85.                 res+=vertex;
  86.                 q.pop();
  87.                 vector<char> children=graph[vertex];
  88.                 for(auto child: children)
  89.                 {
  90.                     inDegree[child]--;
  91.                     if(inDegree[child]==0)
  92.                         q.push(child);
  93.                 }
  94.             }
  95.            
  96.             if(res.size()!=inDegree.size())
  97.                 return "";
  98.                
  99.             return res;        
  100.         }
  101. };
  102.  
  103. int main(int argc, char **argv[])
  104. {
  105.     string res=TopologicalSort::sort(vector<string>{"ywx", "wz", "xz", "zyy", "zwz"});
  106.     cout<<res;
  107. }
  108.  
  109. /* Time Complexity ---->
  110.    # The time complexity of the above algorithm will be O(V+E), where ‘V’ is the total number of different
  111.      characters and ‘E’ is the total number of the rules in the alien language.
  112.    # ∵ at most, each pair of words can give us one rule, therefore, we can conclude that the upper bound for  
  113.      the rules is O(N) where ‘N’ is the number of words in the input.
  114.    # So, the time complexity of our algorithm is O(V+N).
  115.  
  116.    Space Complexity ----->
  117.    # The space complexity will be O(V+N), ∵ we are storing all of the rules for each character in an
  118.      adjacency list.
  119. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement