BeyondCoder: [Leetcode] Alien Dictionary (Graph)
[Leetcode] Alien Dictionary (Graph)
[Problem]
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, where words 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,
Note:
[Algorithm]
Graph, Topological sort, use HashMap to save vertex and its adjancecy list with char as hashkey.
Note:
Be sure to understand the problem: lexi order means *between* up and down words, not chars inside each word.
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, where words 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:
- You may assume all letters are in lowercase.
- If the order is invalid, return an empty string.
- There may be multiple valid order of letters, return any one of them is fine.
[Algorithm]
Graph, Topological sort, use HashMap to save vertex and its adjancecy list with char as hashkey.
Note:
Be sure to understand the problem: lexi order means *between* up and down words, not chars inside each word.
Read full article from BeyondCoder: [Leetcode] Alien Dictionary (Graph)
No comments:
Post a Comment