/** 269. Alien Dictionary * 2016-6-24 by Mingyang * This is a difficult topic, but the idea is very clear, a look at the graph inside the topic, but in the specific details, we also need to consider * first is here the order, I opened Start is a comparison between every two char, there is a map inside, and then each string of the first character and then the vertical comparison * and then with the queue, then remove, one is too complex, and the second is removed when there is no degree of concept, if remove a lot of points * the following code Yes, I saw it more clearly.1190000003795463 */ PublicString Alienorder (string[] words) {//diagram of the node compositionMap<character, set<character>> graph =NewHashmap<character, set<character>>(); //the counter of the nodeMap<character, integer> indegree =NewHashmap<character, integer>(); //there's a result .StringBuilder order =NewStringBuilder (); //initialization graphs and countersInitialize (words, graph, indegree); //Build and CountBuildgraphandgetindegree (words, graph, indegree); //the last step of the topology ordering, based on the value of the counter values breadth First searchTopologicalsort (order, graph, indegree); //If the size is equal, no ring is indicated returnOrder.length () = = Indegree.size ()? Order.tostring (): ""; } Private voidInitialize (string[] words, map<character, set<character>> graph, Map<character, integer>Indegree) { for(String word:words) { for(inti = 0; I < word.length (); i++){ CharCurr =Word.charat (i); //Initialize counter and graph nodes for each letter of each word if(Graph.get (curr) = =NULL) {graph.put (Curr,NewHashset<character>()); } if(Indegree.get (curr) = =NULL) {indegree.put (Curr,0); } } } } Private voidBuildgraphandgetindegree (string[] words, map<character, set<character>> graph, Map<character, Integer >Indegree) {Set<String> edges =NewHashset<string>(); for(inti = 0; i < words.length-1; i++){ //compare each of the two adjacent wordsString word1 =Words[i]; String Word2= words[i + 1]; for(intj = 0; J < Word1.length () && J < Word2.length (); J + +){ Charfrom =Word1.charat (j); Charto =Word2.charat (j); //If the same continues, find two words the first different letter if(from = = to)Continue; //if these two-letter edges have not been used, then if(!edges.contains (from+ "" +to )) {Set<Character> set =Graph.get (from); Set.add (to); //Add The following letters to the set in the preceding letterGraph.put (from, set); Integer Toin=Indegree.get (to); Toin++; //Update the counter with the following letters, +1Indegree.put (to, Toin); //this side of the record has been processed.Edges.add (from+ "" +to ); Break; } } } } Private voidTopologicalsort (StringBuilder order, Map<character, set<character>> graph, Map<character, integer>Indegree) { //Breadth-First search queuequeue<character> queue =NewLinkedlist<character>(); //Join the queue to the root of the graph, that is, a node with a counter of 0 for(Character key:indegree.keySet ()) {if(Indegree.get (key) = = 0) {Queue.offer (key); } } //Search while(!Queue.isempty ()) {Character Curr=Queue.poll (); //Add the Team head node to the resultsorder.append (Curr); Set<Character> set =Graph.get (Curr); if(Set! =NULL){ //for all nodes that the node points to, update its counter, -1 for(Character c:set) {Integer val=Indegree.get (c); Val--; //If the counter is zeroed, the queue is queued for processing if(val = = 0) {Queue.offer (c); } indegree.put (c, Val); } } } }
269. Alien Dictionary