-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlienDictionary.java
72 lines (68 loc) · 1.91 KB
/
AlienDictionary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Solution {
/**
* @param words: a list of words
* @return: a string which is correct order
*/
StringBuilder res;
public String alienOrder(String[] words) {
// Write your code here
List<Character>[] lists = new ArrayList();
for(String s : words)
{
for(char ch : s.toCharArray())
{
if(lists[ch - 'a'] == null)
lists[ch - 'a'] = new LinkedList();
}
}
//Initializing adjacency list
for(int i=1; i<words.length();i++)
{
String s1 = words[i - 1], s2 = words[i];
int min = Math.min(s1.length(), s2.length());
if(s1.length() > s2.length() && areStringEqual(s1,s2,min))
return "";
for(int i=0;i<min;i++)
{
if(s1.charAt(i) != s2.charAt(i))
{
lists[s1.charAt(i)].add(s2.charAt(i));
break;
}
}
}
//Check string equality
//dfs
Boolean[] visited = new Boolean[26];
for(List<Character> list : lists)
{
for(char ch : list)
{
if(DFS(ch,lists, visited))
return "";
}
}
return res.toString();
}
public boolean areStringsEqual(String s1, String s2, int l)
{
for(int i=0;i<l;i++)
{
if(s1.charAt(i) != s2.charAt(i))
return false;
}
return true;
}
public boolean DFS(char ch,List<Character>[] lists,Boolean[] visited )
{
if(visited[ch - 'a'] != null)return visited[ch - 'a'];
visited[ch - 'a'] = true;
for(char neigh : lists[ch - 'a'])
{
if(DFS(neigh, lists, visited))
return true;
}
visited[ch - 'a'] = false;
res.insert(0, ch);
}
}