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.
You may assume all letters are in lowercase.
The dictionary is invalid, if a is prefix of b and b is appear before a.
If the order is invalid, return an empty string.
There may be multiple valid order of letters, return the smallest in normal lexicographical order
Example
Example 1:
Input:["wrt","wrf","er","ett","rftt"]
Output:"wertf"
Explanation:
from "wrt"and"wrf" ,we can get 't'<'f'
from "wrt"and"er" ,we can get 'w'<'e'
from "er"and"ett" ,we can get 'r'<'t'
from "ett"and"rftt" ,we can get 'e'<'r'
So return "wertf"
Example 2:
Input:["z","x"]
Output:"zx"
Explanation:
from "z" and "x",we can get 'z' < 'x'
So return "zx"
publicclassSolution {publicStringalienOrder(String[] words) {ArrayList<Integer>[] graph =newArrayList[26];// Creating graphfor (int i =0; i <words.length; i++) {// Only creating those nodes which existsfor (int j =0; j < words[i].length(); j++)if (graph[words[i].charAt(j) -'a'] ==null) graph[words[i].charAt(j) -'a'] =newArrayList<>();if (i !=0) {int j =0;while (j <Math.min(words[i].length(), words[i -1].length())&& words[i].charAt(j) == words[i -1].charAt(j)) j++;// Direction graph from smaller to bigger char(alien lexically)if (j !=Math.min(words[i].length(), words[i -1].length())) graph[words[i -1].charAt(j) -'a'].add(words[i].charAt(j) -'a'); } }// Creating indegree arrayint[] indegree =newint[26];Arrays.fill(indegree,-1);int count =0;// Indegree =-1 implies that nodes does not existsfor (int i =0; i <26; i++) {if (graph[i] !=null) { indegree[i] =0; count++; } }for (int i =0; i <26; i++) {if (graph[i] !=null) {for (int x : graph[i]) indegree[x]++; } }// Topological SortPriorityQueue<Integer> q =newPriorityQueue<>();for (int i =0; i <26; i++)if (indegree[i] ==0)q.add(i);StringBuilder ans =newStringBuilder();while (q.size() !=0) {char c = (char) (q.poll() +'a');ans.append(c);for (int x : graph[c -'a']) { indegree[x]--;if (indegree[x] ==0)q.add(x); } }// If a cycle exists then return empty stringreturnans.length() == count ?ans.toString() :""; }}