269. Alien Dictionary
https://www.lintcode.com/problem/alien-dictionary/description
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.
Example 1:
Input: [ "wrt", "wrf", "er", "ett", "rftt" ] Output: "wertf"Example 2:
Input: [ "z", "x" ] Output: "zx"Example 3:
Input: [ "z", "x", "z" ] Output: "" Explanation: The order is invalid, so return "".Note:
You may assume all letters are in lowercase.
You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
If the order is invalid, return an empty string.
There may be multiple valid order of letters, return any one of them is fine.
给定一堆词,它们按某种字符顺序排好了序,让求出满足input的一种字符顺序。比如[ "wrt", "wrf", "er", "ett", "rftt" ]
wrt>wrf =>t>f, wrf>er=>w>e,er>et=>r>t,ett>rftt=>e>r, 因此可得出w>e>r>t>f。也就是说给定了一系列顺序,要求返回根据顺序遍历的结果,即拓扑排序。先根据input的单词生成单个字符之间的顺序对,如t>f, r>t等。再对字符顺序对做拓扑排序。
Last updated
Was this helpful?