Skip to content

Commit d1213da

Browse files
update 269
1 parent 124aba1 commit d1213da

File tree

1 file changed

+23
-23
lines changed
  • src/main/java/com/fishercoder/solutions/firstthousand

1 file changed

+23
-23
lines changed

src/main/java/com/fishercoder/solutions/firstthousand/_269.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99

1010
public class _269 {
1111
public static class Solution1 {
12-
1312
/**
1413
* reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs
1514
*/
1615
public String alienOrder(String[] words) {
17-
Map<Character, Set<Character>> map = new HashMap<>();
18-
Map<Character, Integer> degree = new HashMap<>();
19-
String result = "";
16+
Map<Character, Set<Character>> charToSmallerCharsMap = new HashMap<>();//this map means all chars in the value set are after the char in the key
17+
Map<Character, Integer> indegreeMap = new HashMap<>();//this map means how many chars are before this given char
18+
StringBuilder result = new StringBuilder();
2019
if (words == null || words.length == 0) {
21-
return result;
20+
return result.toString();
2221
}
2322
for (String s : words) {
2423
for (char c : s.toCharArray()) {
25-
degree.put(c, 0);
24+
//we go through each word, nothing to compare, so each char's degree should be zero
25+
//only when we compare words[i] with words[i+1], we know the order of different chars
26+
indegreeMap.put(c, 0);
2627
}
2728
}
2829
for (int i = 0; i < words.length - 1; i++) {
@@ -31,46 +32,45 @@ public String alienOrder(String[] words) {
3132
if (curr.length() > next.length() && curr.startsWith(next)) {
3233
return "";
3334
}
34-
int minLen = Math.min(curr.length(), next.length());
35-
for (int j = 0; j < minLen; j++) {
35+
for (int j = 0; j < curr.length(); j++) {
3636
char c1 = curr.charAt(j);
3737
char c2 = next.charAt(j);
3838
if (c1 != c2) {
39-
Set<Character> set = new HashSet<>();
40-
if (map.containsKey(c1)) {
41-
set = map.get(c1);
42-
}
39+
Set<Character> set = charToSmallerCharsMap.getOrDefault(c1, new HashSet<>());
4340
if (!set.contains(c2)) {
4441
set.add(c2);
45-
map.put(c1, set);
46-
degree.put(c2, degree.get(c2) + 1);
42+
charToSmallerCharsMap.put(c1, set);
43+
indegreeMap.put(c2, indegreeMap.get(c2) + 1);
4744
}
45+
//no longer need to continue iterating through either one of these two words and should not to,
46+
//because the first two chars at the same position of these two words that differ decides the order
4847
break;
4948
}
5049
}
5150
}
5251
Queue<Character> queue = new LinkedList<>();
53-
for (char c : degree.keySet()) {
54-
if (degree.get(c) == 0) {
52+
for (char c : indegreeMap.keySet()) {
53+
if (indegreeMap.get(c) == 0) {
54+
//this means no chars come before this char, so they should be at the head of this alien dictionary
5555
queue.offer(c);
5656
}
5757
}
5858
while (!queue.isEmpty()) {
5959
char curr = queue.poll();
60-
result += curr;
61-
if (map.containsKey(curr)) {
62-
for (char c : map.get(curr)) {
63-
degree.put(c, degree.get(c) - 1);
64-
if (degree.get(c) == 0) {
60+
result.append(curr);
61+
if (charToSmallerCharsMap.containsKey(curr)) {
62+
for (char c : charToSmallerCharsMap.get(curr)) {
63+
indegreeMap.put(c, indegreeMap.get(c) - 1);
64+
if (indegreeMap.get(c) == 0) {
6565
queue.offer(c);
6666
}
6767
}
6868
}
6969
}
70-
if (result.length() != degree.size()) {
70+
if (result.length() != indegreeMap.size()) {
7171
return "";
7272
}
73-
return result;
73+
return result.toString();
7474
}
7575
}
7676

0 commit comments

Comments
 (0)