forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_269.java
103 lines (95 loc) · 3.13 KB
/
_269.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
/**
* 269. Alien Dictionary
*
* 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.
*/
public class _269 {
/**reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs*/
public static String alienOrder(String[] words) {
Map<Character, Set<Character>> map = new HashMap();
Map<Character, Integer> degree = new HashMap<>();
String result = "";
if (words == null || words.length == 0) {
return result;
}
for (String s : words) {
for (char c : s.toCharArray()) {
degree.put(c, 0);//keeps overwriting it, the purpose is to create one entry
//for each letter in the degree map
}
}
for (int i = 0; i < words.length - 1; i++) {
String cur = words[i];
String next = words[i + 1];
int length = Math.min(cur.length(), next.length());
for (int j = 0; j < length; j++) {
char c1 = cur.charAt(j);
char c2 = next.charAt(j);
if (c1 != c2) {
Set<Character> set = new HashSet<>();
if (map.containsKey(c1)) {
set = map.get(c1);
}
if (!set.contains(c2)) {
set.add(c2);
map.put(c1, set);
degree.put(c2, degree.get(c2) + 1);
}
break;
}
}
}
Queue<Character> queue = new LinkedList<>();
for (char c : degree.keySet()) {
if (degree.get(c) == 0) {
queue.add(c);
}
}
while (!queue.isEmpty()) {
char c = queue.remove();
result += c;
if (map.containsKey(c)) {
for (char c2 : map.get(c)) {
degree.put(c2, degree.get(c2) - 1);
if (degree.get(c2) == 0) {
queue.add(c2);
}
}
}
}
if (result.length() != degree.size()) {
return "";
}
return result;
}
public static void main(String... args) {
System.out.println("Hello world.");
String[] words = new String[]{"wrt", "wrf", "er", "ett", "rftt"};
String order = alienOrder(words);
System.out.print(order);
}
}