描述
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 non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
1 | You may assume all letters are in lowercase. |
Example:
1 | Input:["wrt","wrf","er","ett","rftt"] |
思路
典型拓扑排序. 因为返回可能的结果中字典序最小的, 在建立res
数组时, 队列的pop顺序要从小到大. 所以队列用pq(heap).
代码
1 | import heapq |