269. Alien Dictionary

Source: Internet
Author: User

Topic:

There is a new alien language which uses the Latin alphabet. However, the order among letters is unknown to you. You receive a list of words from the dictionary, Wherewords is sorted lexicographically by the rules of this new language . Derive the order of letters in this language.

For example,
Given the following words in dictionary,

[  "WRT",  "WRF",  "er",  "ett",  "RFTT"]

The correct order is: "wertf" .

Note:

    1. Assume all letters is in lowercase.
    2. If The order is invalid, return an empty string.
    3. There may multiple valid order of letters and return any one of the them is fine.

Links: http://leetcode.com/problems/alien-dictionary/

Exercises

This problem is also card for a long time, because test instructions understanding is not clear. method or topological sorting, the meaning of this question is-Words is sorted, with each word in the letter order does not matter. Just like "Apple" is in front of "banana", but not "a" in "Apple" is in front of "P". So it took a lot of time to construct edges in the order of each word, and the result was wrong. This kind of thing is in the final analysis my own English is not good. Wrote a lot of always pass, found that the real meaning of the topic after a lot of, after the interview will be more exchanges and more communication, lest test instructions not clear white waste time to do the problem. In addition, for the diagram, the figure of three ways of expression, list of edges, ajacency matrix and Ajacency lists must be clear, more understanding.

Kahn ' s algorithm: first convert the input words to adjacency lists or list of edges, then course Schedule using topological sorting II method. Here we use Kahn's algorithm, calculate Indegree first, then maintain a queue to carry out BFS.

Time Complexity-o (ve), Space complexity-o (VE)

 Public classSolution { PublicString Alienorder (string[] words) {//topological Sorting-kahn ' s algorithm        if(Words = =NULL|| Words.length = = 0) {            return""; } Map<character, hashset<character>> map =NewHashmap<>(); Map<character, integer> indegree =NewHashmap<>();  for(String s:words) { for(inti = 0; I < s.length (); i++) {                Charc =S.charat (i); Indegree.put (c,0); }        }                 for(inti = 1; i < words.length; i++) {//Find (Prevchar, curchar) pairs as edgesString prevstr = words[i-1]; String Curstr=Words[i]; intLen =math.min (Prevstr.length (), curstr.length ());  for(intj = 0; J < Len; J + +) {                CharCurchar =Curstr.charat (j); CharPrevchar =Prevstr.charat (j); if(Curchar = =Prevchar) {                    Continue; } Else{//find edges;                    if(Map.containskey (Prevchar)) {if(!Map.get (Prevchar). Contains (Curchar))                            {Map.get (Prevchar). Add (Curchar); Indegree.put (Curchar, Indegree.get (Curchar)+ 1); }                    } Else{HashSet<Character> Tmpset =NewHashset<>();                        Tmpset.add (Curchar);                        Map.put (Prevchar, Tmpset); Indegree.put (Curchar, Indegree.get (Curchar)+ 1); }                     Break; }}} Queue<Character> queue =NewLinkedlist<>();  for(CharC:indegree.keyset ()) {            if(Indegree.get (c) = = 0) {Queue.offer (c); }} StringBuilder Res=NewStringBuilder ();  while(!Queue.isempty ()) {            Charc =Queue.poll ();            Res.append (c); if(Map.containskey (c)) { for(CharL:map.get (c)) {Indegree.put (L, Indegree.get (L)-1); if(Indegree.get (l) = = 0) {queue.offer (L); }                }            }        }                if(Res.length ()! =indegree.size ()) {            return""; }                returnres.tostring (); }}

Off Topic:

The food is too rich these days. Friday BCD Tofu, Saturday Liu Hotpot (renamed), Sunday went to flushing to eat the old sheep Hall, the result is slack. But it's also good to have fun with our little friends and relax. The decoration of the house is nearing the end, and tomorrow Monday is expected to be finished, hoping everything goes well. At the same time, tomorrow also began a week-long training, mainly modern Web development, speaking JavaScript. At the same time Coursera also has a lecture on Angularjs, the Hong Kong University of Science and Technology opened tomorrow, the first day of the start. Take this opportunity to learn some angularjs.

Reference:

Https://leetcode.com/discuss/53997/the-description-is-wrong

Https://leetcode.com/discuss/71991/8ms-clean-java-using-topological-sort-and-dfs

Https://leetcode.com/discuss/65274/java-solution-basic-topological-sort

Https://leetcode.com/discuss/69894/fastest-java-solution-topological-comments-improvements

Https://leetcode.com/discuss/54002/simple-idea-based-on-dfs-4ms-in-c

Https://leetcode.com/discuss/54549/java-toposort-solution-clean

Https://leetcode.com/discuss/54188/16-18-lines-python-29-lines-c

269. Alien Dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.