269. Alien Dictionary

https://leetcode.com/problems/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:
  1. You may assume all letters are in lowercase.
  2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
  3. If the order is invalid, return an empty string.
  4. There may be multiple valid order of letters, return any one of them is fine.

---






Intuition
Topo sort, each valid different char defines inDegree
Can use array for inDegree and graph since array access is faster than map
Use null check for when to init graph list, and inDegree, and count distinct # of chars

if output length < # distinct chars => return ""

Also bounds check if input list of words is not lexicographically ordered => check that shorter words are before longer in case of common prefixes
---
Time - O(N log N) - PQ to get shortest lexicographic string
Space - O(N) - number of words - graph size, inDegree size
---
Related problems
verifying-alien-dictionary
course-schedule-ii
---