@@ -30,13 +30,17 @@ public static boolean isAlienSorted(String[] words, String order) {
3030 }
3131
3232 private static boolean checkTwoWords (String current , String next , String order ) {
33+
3334 Map <Character , Integer > map = new HashMap <>();
3435 for (int i = 1 ; i <= 26 ; i ++) {
3536 map .put (order .charAt (i - 1 ), i );
3637 }
3738
3839 for (int idx = 0 ; idx < current .length (); idx ++) {
3940 char ch1 = current .charAt (idx );
41+ // if not matched yet but next string is traversed already
42+ if (idx == next .length ())
43+ return false ;
4044 char ch2 = next .charAt (idx );
4145
4246 int order1 = map .get (ch1 );
@@ -54,9 +58,18 @@ else if (order1 > order2)
5458 }
5559
5660 public static void main (String [] args ) {
57- String [] words = { "hello" , "leetcode" };
58- String order = "worldabcefghijkmnpqstuvxyz" ;
5961
62+ String [] words2 = { "hello" , "leetcode" };
63+ String order2 = "hlabcdefgijkmnopqrstuvwxyz" ;
64+ System .out .println (isAlienSorted (words2 , order2 ));
65+
66+ String [] words = { "word" , "world" , "row" };
67+ String order = "worldabcefghijkmnpqstuvxyz" ;
6068 System .out .println (isAlienSorted (words , order ));
69+
70+ String [] words3 = { "apple" , "app" };
71+ // String[] words3 = { "apap", "app" };
72+ String order3 = "abcdefghijklmnopqrstuvwxyz" ;
73+ System .out .println (isAlienSorted (words3 , order3 ));
6174 }
6275}
0 commit comments