Skip to content

Commit 0bd5f12

Browse files
add 953
1 parent a78bdac commit 0bd5f12

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Your ideas/fixes/algorithms are more than welcome!
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3232
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion
3333
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
34+
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java) | O(1) | O(1) | |Easy|
3435
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
3536
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
3637
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | O(n) | O(n) | |Easy|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 953. Verifying an Alien Dictionary
8+
*
9+
* In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
10+
*
11+
* Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.
12+
*
13+
* Example 1:
14+
*
15+
* Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
16+
* Output: true
17+
* Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
18+
*
19+
* Example 2:
20+
*
21+
* Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
22+
* Output: false
23+
* Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
24+
*
25+
* Example 3:
26+
*
27+
* Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
28+
* Output: false
29+
* Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
30+
*
31+
*
32+
* Note:
33+
* 1 <= words.length <= 100
34+
* 1 <= words[i].length <= 20
35+
* order.length == 26
36+
* All characters in words[i] and order are english lowercase letters.
37+
*/
38+
public class _953 {
39+
public static class Solution1 {
40+
public boolean isAlienSorted(String[] words, String order) {
41+
if (words.length == 1) {
42+
return true;
43+
}
44+
45+
Map<Character, Integer> map = new HashMap<>();
46+
for (int i = 0; i < order.length(); i++) {
47+
map.put(order.charAt(i), i);
48+
}
49+
50+
for (int i = 0; i < words.length - 1; i++) {
51+
String firstWord = words[i];
52+
String secondWord = words[i + 1];
53+
if (!sorted(firstWord, secondWord, map)) {
54+
return false;
55+
}
56+
}
57+
return true;
58+
}
59+
60+
private boolean sorted(String firstWord, String secondWord, Map<Character, Integer> map) {
61+
for (int i = 0; i < Math.min(firstWord.length(), secondWord.length()); i++) {
62+
if (firstWord.charAt(i) == secondWord.charAt(i)) {
63+
continue;
64+
} else {
65+
if (map.get(firstWord.charAt(i)) > map.get(secondWord.charAt(i))) {
66+
return false;
67+
} else {
68+
return true;
69+
}
70+
}
71+
}
72+
return firstWord.length() <= secondWord.length();
73+
}
74+
}
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._953;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _953Test {
10+
private static _953.Solution1 solution1;
11+
private static String[] words;
12+
private static String order;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _953.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
words = new String[] {"hello", "leetcode"};
22+
order = "hlabcdefgijkmnopqrstuvwxyz";
23+
assertEquals(true, solution1.isAlienSorted(words, order));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
words = new String[] {"word", "world", "row"};
29+
order = "worldabcefghijkmnpqstuvxyz";
30+
assertEquals(false, solution1.isAlienSorted(words, order));
31+
}
32+
33+
@Test
34+
public void test3() {
35+
words = new String[] {"apple", "app"};
36+
order = "abcdefghijklmnopqrstuvwxyz";
37+
assertEquals(false, solution1.isAlienSorted(words, order));
38+
}
39+
}

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