Skip to content

Commit 2e25494

Browse files
add 269
1 parent b2aa7d4 commit 2e25494

File tree

3 files changed

+87
-101
lines changed

3 files changed

+87
-101
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ Your ideas/fixes/algorithms are more than welcome!
298298
|272|[Closest Binary Search Tree Value II](https://leetcode.com/problems/closest-binary-search-tree-value-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ClosestBinarySearchTreeValueII.java)| O(h+k)|O(h) | Hard| Stack
299299
|271|[Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/EncodeandDecodeStrings.java)| O(n)|O(1) | Medium|
300300
|270|[Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_270.java)| O(h)|O(1) | Easy| DFS
301+
|269|[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_269.java)| O(?)|O(?) | Hard| Topological Sort
301302
|268|[Missing Number](https://leetcode.com/problems/missing-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_268.java)| O(n)|O(1) | Easy| Bit Manipulation
302303
|267|[Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/PalindromePermutationII.java)| O(n*n!)|O(n) | Medium|
303304
|266|[Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/PalindromePermutation.java)| O(n)|O(1) | Easy|

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

Lines changed: 0 additions & 101 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 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.
7+
8+
For example,
9+
Given the following words in dictionary,
10+
11+
[
12+
"wrt",
13+
"wrf",
14+
"er",
15+
"ett",
16+
"rftt"
17+
]
18+
The correct order is: "wertf".
19+
20+
Note:
21+
You may assume all letters are in lowercase.
22+
If the order is invalid, return an empty string.
23+
There may be multiple valid order of letters, return any one of them is fine.
24+
*/
25+
public class _269 {
26+
27+
/**reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs*/
28+
public static String alienOrder(String[] words) {
29+
Map<Character, Set<Character>> map = new HashMap();
30+
Map<Character, Integer> degree = new HashMap<>();
31+
String result = "";
32+
if (words == null || words.length == 0) return result;
33+
for (String s : words) {
34+
for (char c : s.toCharArray()) {
35+
degree.put(c, 0);//keeps overwriting it, the purpose is to create one entry
36+
//for each letter in the degree map
37+
}
38+
}
39+
for (int i = 0; i < words.length - 1; i++) {
40+
String cur = words[i];
41+
String next = words[i + 1];
42+
int length = Math.min(cur.length(), next.length());
43+
for (int j = 0; j < length; j++) {
44+
char c1 = cur.charAt(j);
45+
char c2 = next.charAt(j);
46+
if (c1 != c2) {
47+
Set<Character> set = new HashSet<>();
48+
if (map.containsKey(c1)) {
49+
set = map.get(c1);
50+
}
51+
if (!set.contains(c2)) {
52+
set.add(c2);
53+
map.put(c1, set);
54+
degree.put(c2, degree.get(c2) + 1);
55+
}
56+
break;
57+
}
58+
}
59+
}
60+
Queue<Character> queue = new LinkedList<>();
61+
for (char c : degree.keySet()) {
62+
if (degree.get(c) == 0) queue.add(c);
63+
}
64+
while (!queue.isEmpty()) {
65+
char c = queue.remove();
66+
result += c;
67+
if (map.containsKey(c)) {
68+
for (char c2 : map.get(c)) {
69+
degree.put(c2, degree.get(c2) - 1);
70+
if (degree.get(c2) == 0) {
71+
queue.add(c2);
72+
}
73+
}
74+
}
75+
}
76+
if (result.length() != degree.size()) return "";
77+
return result;
78+
}
79+
80+
public static void main(String ... args){
81+
System.out.println("Hello world.");
82+
String[] words = new String[]{"wrt","wrf","er","ett","rftt"};
83+
String order = alienOrder(words);
84+
System.out.print(order);
85+
}
86+
}

0 commit comments

Comments
 (0)