标签:
题目:
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, wherewords are 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:
链接: http://leetcode.com/problems/alien-dictionary/
题解:
这道题也是卡了很久,原因在于题意理解不明确。方法还是Topological Sorting,这题的意思是 - Words are sorted, 跟每个Word里面的letter顺序并没有什么关系。就比如"apple"排在"banana"前面,但并不是"apple"里面的"a"就排在"p"前面。所以之前花了不少时间根据每个单词内部的顺序构造edges,结果都是错的。 这种东西归根结底还是我自己的英语不好。写了一大堆总是通不过,发现题目真正含义以后就好很多了,以后面试的话也要多交流多沟通,免得题意弄不清楚白浪费时间做题。另外,对于图来说,图的三种表达方式, list of edges, ajacency matrix和ajacency lists一定要搞清楚,多弄明白。
Kahn‘s Algorithm: 先把输入words转化为Adjacency Lists或者list of edges,然后使用Course Schedule II的方法进行Topological Sorting。这里我们使用Kahn‘s Algorithm,先计算inDegree,然后维护一个Queue来进行BFS。
Time Complexity - O(VE),Space Complexity - O(VE)
public class Solution { public String alienOrder(String[] words) { // Topological sorting - Kahn‘s Algorithm if(words == null || words.length == 0) { return ""; } Map<Character, HashSet<Character>> map = new HashMap<>(); Map<Character, Integer> inDegree = new HashMap<>(); for(String s : words) { for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); inDegree.put(c, 0); } } for(int i = 1; i < words.length; i++) { // find (prevChar, curChar) pairs as edges String prevStr = words[i - 1]; String curStr = words[i]; int len = Math.min(prevStr.length(), curStr.length()); for(int j = 0; j < len; j++) { char curChar = curStr.charAt(j); char prevChar = 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 = new HashSet<>(); tmpSet.add(curChar); map.put(prevChar, tmpSet); inDegree.put(curChar, inDegree.get(curChar) + 1); } break; } } } Queue<Character> queue = new LinkedList<>(); for(char c : inDegree.keySet()) { if(inDegree.get(c) == 0) { queue.offer(c); } } StringBuilder res = new StringBuilder(); while(!queue.isEmpty()) { char c = queue.poll(); res.append(c); if(map.containsKey(c)) { for(char l : map.get(c)) { inDegree.put(l, inDegree.get(l) - 1); if(inDegree.get(l) == 0) { queue.offer(l); } } } } if(res.length() != inDegree.size()) { return ""; } return res.toString(); } }
题外话:
这几天食物过于丰富。周五BCD豆腐,周六刘一手火锅(改名了),周日又去了法拉盛吃老周全羊馆,结果就懈怠了。不过和小伙伴们玩玩三国杀放松一下也是很好的。房子的装修也接近尾声,明天周一估计就可以收工了,希望一切顺利。 同时,明天也开始为期一周的培训,主要是Modern Web Development,讲JavaScript的。同时Coursera也有一门讲AngularJS的课程,香港科技大学开设的,明天开始第一天。要趁这个机会好好学一些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
标签:
原文地址:http://www.cnblogs.com/yrbbest/p/5023584.html