Skip to content

Commit ae2e783

Browse files
add a solution for 953
1 parent 4634824 commit ae2e783

File tree

2 files changed

+61
-25
lines changed

2 files changed

+61
-25
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Arrays;
4+
import java.util.Comparator;
35
import java.util.HashMap;
46
import java.util.Map;
57

@@ -40,4 +42,33 @@ private boolean sorted(String firstWord, String secondWord, Map<Character, Integ
4042
return firstWord.length() <= secondWord.length();
4143
}
4244
}
45+
46+
public static class Solution2 {
47+
public boolean isAlienSorted(String[] words, String order) {
48+
String[] copy = Arrays.copyOf(words, words.length);
49+
Arrays.sort(words, new Comparator<String>() {
50+
@Override
51+
public int compare(String o1, String o2) {
52+
int pos1 = 0;
53+
int pos2 = 0;
54+
for (int i = 0; i < Math.min(o1.length(), o2.length()) && pos1 == pos2; i++) {
55+
pos1 = order.indexOf(o1.charAt(i));
56+
pos2 = order.indexOf(o2.charAt(i));
57+
}
58+
59+
if (pos1 == pos2 && o1.length() != o2.length()) {
60+
return o1.length() - o2.length();
61+
}
62+
63+
return pos1 - pos2;
64+
}
65+
});
66+
for (int i = 0; i < words.length; i++) {
67+
if (!copy[i].equals(words[i])) {
68+
return false;
69+
}
70+
}
71+
return true;
72+
}
73+
}
4374
}

src/test/java/com/fishercoder/_953Test.java

+30-25
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,38 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _953Test {
10-
private static _953.Solution1 solution1;
11-
private static String[] words;
12-
private static String order;
10+
private static _953.Solution1 solution1;
11+
private static _953.Solution2 solution2;
12+
private static String[] words;
13+
private static String order;
1314

14-
@BeforeClass
15-
public static void setup() {
16-
solution1 = new _953.Solution1();
17-
}
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _953.Solution1();
18+
solution2 = new _953.Solution2();
19+
}
1820

19-
@Test
20-
public void test1() {
21-
words = new String[] {"hello", "leetcode"};
22-
order = "hlabcdefgijkmnopqrstuvwxyz";
23-
assertEquals(true, solution1.isAlienSorted(words, order));
24-
}
21+
@Test
22+
public void test1() {
23+
words = new String[]{"hello", "leetcode"};
24+
order = "hlabcdefgijkmnopqrstuvwxyz";
25+
assertEquals(true, solution1.isAlienSorted(words, order));
26+
assertEquals(true, solution2.isAlienSorted(words, order));
27+
}
2528

26-
@Test
27-
public void test2() {
28-
words = new String[] {"word", "world", "row"};
29-
order = "worldabcefghijkmnpqstuvxyz";
30-
assertEquals(false, solution1.isAlienSorted(words, order));
31-
}
29+
@Test
30+
public void test2() {
31+
words = new String[]{"word", "world", "row"};
32+
order = "worldabcefghijkmnpqstuvxyz";
33+
assertEquals(false, solution1.isAlienSorted(words, order));
34+
assertEquals(false, solution2.isAlienSorted(words, order));
35+
}
3236

33-
@Test
34-
public void test3() {
35-
words = new String[] {"apple", "app"};
36-
order = "abcdefghijklmnopqrstuvwxyz";
37-
assertEquals(false, solution1.isAlienSorted(words, order));
38-
}
37+
@Test
38+
public void test3() {
39+
words = new String[]{"apple", "app"};
40+
order = "abcdefghijklmnopqrstuvwxyz";
41+
assertEquals(false, solution1.isAlienSorted(words, order));
42+
assertEquals(false, solution2.isAlienSorted(words, order));
43+
}
3944
}

0 commit comments

Comments
 (0)