|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import java.util.HashMap;
|
5 | 4 | import java.util.HashSet;
|
6 | 5 | import java.util.LinkedList;
|
|
34 | 33 | There may be multiple valid order of letters, return any one of them is fine.
|
35 | 34 | */
|
36 | 35 | public class _269 {
|
| 36 | + public static class Solution1 { |
37 | 37 |
|
38 |
| - /**reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs*/ |
39 |
| - public static String alienOrder(String[] words) { |
40 |
| - Map<Character, Set<Character>> map = new HashMap(); |
41 |
| - Map<Character, Integer> degree = new HashMap<>(); |
42 |
| - String result = ""; |
43 |
| - if (words == null || words.length == 0) { |
44 |
| - return result; |
45 |
| - } |
46 |
| - for (String s : words) { |
47 |
| - for (char c : s.toCharArray()) { |
48 |
| - degree.put(c, 0);//keeps overwriting it, the purpose is to create one entry |
49 |
| - //for each letter in the degree map |
| 38 | + /** |
| 39 | + * reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs |
| 40 | + */ |
| 41 | + public String alienOrder(String[] words) { |
| 42 | + Map<Character, Set<Character>> map = new HashMap(); |
| 43 | + Map<Character, Integer> degree = new HashMap<>(); |
| 44 | + String result = ""; |
| 45 | + if (words == null || words.length == 0) { |
| 46 | + return result; |
50 | 47 | }
|
51 |
| - } |
52 |
| - for (int i = 0; i < words.length - 1; i++) { |
53 |
| - String cur = words[i]; |
54 |
| - String next = words[i + 1]; |
55 |
| - int length = Math.min(cur.length(), next.length()); |
56 |
| - for (int j = 0; j < length; j++) { |
57 |
| - char c1 = cur.charAt(j); |
58 |
| - char c2 = next.charAt(j); |
59 |
| - if (c1 != c2) { |
60 |
| - Set<Character> set = new HashSet<>(); |
61 |
| - if (map.containsKey(c1)) { |
62 |
| - set = map.get(c1); |
63 |
| - } |
64 |
| - if (!set.contains(c2)) { |
65 |
| - set.add(c2); |
66 |
| - map.put(c1, set); |
67 |
| - degree.put(c2, degree.get(c2) + 1); |
| 48 | + for (String s : words) { |
| 49 | + for (char c : s.toCharArray()) { |
| 50 | + degree.put(c, 0);//keeps overwriting it, the purpose is to create one entry |
| 51 | + //for each letter in the degree map |
| 52 | + } |
| 53 | + } |
| 54 | + for (int i = 0; i < words.length - 1; i++) { |
| 55 | + String cur = words[i]; |
| 56 | + String next = words[i + 1]; |
| 57 | + int length = Math.min(cur.length(), next.length()); |
| 58 | + for (int j = 0; j < length; j++) { |
| 59 | + char c1 = cur.charAt(j); |
| 60 | + char c2 = next.charAt(j); |
| 61 | + if (c1 != c2) { |
| 62 | + Set<Character> set = new HashSet<>(); |
| 63 | + if (map.containsKey(c1)) { |
| 64 | + set = map.get(c1); |
| 65 | + } |
| 66 | + if (!set.contains(c2)) { |
| 67 | + set.add(c2);
EDBE
code> |
| 68 | + map.put(c1, set); |
| 69 | + degree.put(c2, degree.get(c2) + 1); |
| 70 | + } |
| 71 | + break; |
68 | 72 | }
|
69 |
| - break; |
70 | 73 | }
|
71 | 74 | }
|
72 |
| - } |
73 |
| - Queue<Character> queue = new LinkedList<>(); |
74 |
| - for (char c : degree.keySet()) { |
75 |
| - if (degree.get(c) == 0) { |
76 |
| - queue.add(c); |
| 75 | + Queue<Character> queue = new LinkedList<>(); |
| 76 | + for (char c : degree.keySet()) { |
| 77 | + if (degree.get(c) == 0) { |
| 78 | + queue.add(c); |
| 79 | + } |
77 | 80 | }
|
78 |
| - } |
79 |
| - while (!queue.isEmpty()) { |
80 |
| - char c = queue.remove(); |
81 |
| - result += c; |
82 |
| - if (map.containsKey(c)) { |
83 |
| - for (char c2 : map.get(c)) { |
84 |
| - degree.put(c2, degree.get(c2) - 1); |
85 |
| - if (degree.get(c2) == 0) { |
86 |
| - queue.add(c2); |
| 81 | + while (!queue.isEmpty()) { |
| 82 | + char c = queue.remove(); |
| 83 | + result += c; |
| 84 | + if (map.containsKey(c)) { |
| 85 | + for (char c2 : map.get(c)) { |
| 86 | + degree.put(c2, degree.get(c2) - 1); |
| 87 | + if (degree.get(c2) == 0) { |
| 88 | + queue.add(c2); |
| 89 | + } |
87 | 90 | }
|
88 | 91 | }
|
89 | 92 | }
|
| 93 | + if (result.length() != degree.size()) { |
| 94 | + return ""; |
| 95 | + } |
| 96 | + return result; |
90 | 97 | }
|
91 |
| - if (result.length() != degree.size()) { |
92 |
| - return ""; |
93 |
| - } |
94 |
| - return result; |
95 | 98 | }
|
96 | 99 |
|
97 |
| - public static void main(String... args) { |
98 |
| - System.out.println("Hello world."); |
99 |
| - String[] words = new String[]{"wrt", "wrf", "er", "ett", "rftt"}; |
100 |
| - String order = alienOrder(words); |
101 |
| - System.out.print(order); |
102 |
| - } |
103 | 100 | }
|
0 commit comments