Skip to content

Commit 09b1b99

Browse files
update 269
1 parent 4e1241b commit 09b1b99

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,26 @@ public static class Solution1 {
1414
* reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs
1515
*/
1616
public String alienOrder(String[] words) {
17-
Map<Character, Set<Character>> map = new HashMap();
17+
Map<Character, Set<Character>> map = new HashMap<>();
1818
Map<Character, Integer> degree = new HashMap<>();
1919
String result = "";
2020
if (words == null || words.length == 0) {
2121
return result;
2222
}
2323
for (String s : words) {
2424
for (char c : s.toCharArray()) {
25-
degree.put(c, 0);//keeps overwriting it, the purpose is to create one entry
26-
//for each letter in the degree map
25+
degree.put(c, 0);
2726
}
2827
}
2928
for (int i = 0; i < words.length - 1; i++) {
30-
String cur = words[i];
29+
String curr = words[i];
3130
String next = words[i + 1];
32-
int length = Math.min(cur.length(), next.length());
33-
for (int j = 0; j < length; j++) {
34-
char c1 = cur.charAt(j);
31+
if (curr.length() > next.length() && curr.startsWith(next)) {
32+
return "";
33+
}
34+
int minLen = Math.min(curr.length(), next.length());
35+
for (int j = 0; j < minLen; j++) {
36+
char c1 = curr.charAt(j);
3537
char c2 = next.charAt(j);
3638
if (c1 != c2) {
3739
Set<Character> set = new HashSet<>();
@@ -50,17 +52,17 @@ public String alienOrder(String[] words) {
5052
Queue<Character> queue = new LinkedList<>();
5153
for (char c : degree.keySet()) {
5254
if (degree.get(c) == 0) {
53-
queue.add(c);
55+
queue.offer(c);
5456
}
5557
}
5658
while (!queue.isEmpty()) {
57-
char c = queue.remove();
58-
result += c;
59-
if (map.containsKey(c)) {
60-
for (char c2 : map.get(c)) {
61-
degree.put(c2, degree.get(c2) - 1);
62-
if (degree.get(c2) == 0) {
63-
queue.add(c2);
59+
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) {
65+
queue.offer(c);
6466
}
6567
}
6668
}

src/test/java/com/fishercoder/_269Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ public void test1() {
2121
assertEquals("wertf", solution1.alienOrder(words));
2222
}
2323

24+
@Test
25+
public void test2() {
26+
words = new String[]{"abc", "ab"};
27+
assertEquals("", solution1.alienOrder(words));
28+
}
29+
2430
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy