package com.fishercoder.solutions;

import java.util.HashMap;
import java.util.Map;

/**
 * 953. Verifying an Alien Dictionary
 *
 * 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.
 *
 * 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.
 *
 * Example 1:
 *
 * Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
 * Output: true
 * Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
 *
 * Example 2:
 *
 * Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
 * Output: false
 * Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
 *
 * Example 3:
 *
 * Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
 * Output: false
 * 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).
 *
 *
 * Note:
 * 1 <= words.length <= 100
 * 1 <= words[i].length <= 20
 * order.length == 26
 * All characters in words[i] and order are english lowercase letters.
 */
public class _953 {
  public static class Solution1 {
    public boolean isAlienSorted(String[] words, String order) {
      if (words.length == 1) {
        return true;
      }

      Map<Character, Integer> map = new HashMap<>();
      for (int i = 0; i < order.length(); i++) {
        map.put(order.charAt(i), i);
      }

      for (int i = 0; i < words.length - 1; i++) {
        String firstWord = words[i];
        String secondWord = words[i + 1];
        if (!sorted(firstWord, secondWord, map)) {
          return false;
        }
      }
      return true;
    }

    private boolean sorted(String firstWord, String secondWord, Map<Character, Integer> map) {
      for (int i = 0; i < Math.min(firstWord.length(), secondWord.length()); i++) {
        if (firstWord.charAt(i) == secondWord.charAt(i)) {
          continue;
        } else {
          if (map.get(firstWord.charAt(i)) > map.get(secondWord.charAt(i))) {
            return false;
          } else {
            return true;
          }
        }
      }
      return firstWord.length() <= secondWord.length();
    }
  }
}

@@ -82,1455 +82,68 @@
 p;%0A%0A
-/**%0A * 953. Verifying an Alien Dictionary%0A *%0A * 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.%0A *%0A * 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.%0A *%0A * Example 1:%0A *%0A * Input: words = %5B%22hello%22,%22leetcode%22%5D, order = %22hlabcdefgijkmnopqrstuvwxyz%22%0A * Output: true%0A * Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.%0A *%0A * Example 2:%0A *%0A * Input: words = %5B%22word%22,%22world%22,%22row%22%5D, order = %22worldabcefghijkmnpqstuvxyz%22%0A * Output: false%0A * Explanation: As 'd' comes after 'l' in this language, then words%5B0%5D %3E words%5B1%5D, hence the sequence is unsorted.%0A *%0A * Example 3:%0A *%0A * Input: words = %5B%22apple%22,%22app%22%5D, order = %22abcdefghijklmnopqrstuvwxyz%22%0A * Output: false%0A * Explanation: The first three characters %22app%22 match, and the second string is shorter (in size.) According to lexicographical rules %22apple%22 %3E %22app%22, because 'l' %3E '%E2%88%85', where '%E2%88%85' is defined as the blank character which is less than any other character (More info).%0A *%0A *%0A * Note:%0A * 1 %3C= words.length %3C= 100%0A * 1 %3C= words%5Bi%5D.length %3C= 20%0A * order.length == 26%0A * All characters in words%5Bi%5D and order are english lowercase letters.%0A */%0Apublic class _953 %7B%0A  public static class Solution1 %7B%0A
+public class _953 %7B%0A    public static class Solution1 %7B%0A    
     
@@ -199,24 +199,30 @@
 ng order) %7B%0A
+      
       if (wo
@@ -240,32 +240,40 @@
 == 1) %7B%0A        
+     
+   
 return true;%0A   
@@ -275,25 +275,37 @@
 ;%0A      
-%7D%0A%0A
+      %7D%0A%0A   
       
+   
 Map%3CChar
@@ -345,24 +345,30 @@
 %3C%3E();%0A      
+    
+  
 for (int i =
@@ -406,16 +406,24 @@
         
+    
+    
 map.put(
@@ -449,25 +449,37 @@
 ;%0A      
-%7D%0A%0A
+      %7D%0A%0A   
       
+   
 for (int
@@ -507,32 +507,40 @@
 gth - 1; i++) %7B%0A
+        
         String f
@@ -564,24 +564,32 @@
 i%5D;%0A        
+  
+      
 String secon
@@ -614,24 +614,32 @@
 1%5D;%0A        
+      
+  
 if (!sorted(
@@ -665,24 +665,34 @@
 rd, map)) %7B%0A
+          
           re
@@ -707,34 +707,48 @@
 se;%0A        
+  
-%7D%0A
+      %7D%0A      
       %7D%0A
       return
@@ -727,32 +727,38 @@
 %7D%0A            %7D%0A
+      
       return tru
@@ -764,23 +764,31 @@
 ue;%0A    
-%7D%0A%0A
+    %7D%0A%0A   
     
+ 
 private 
@@ -866,24 +866,30 @@
 ger%3E map) %7B%0A
+      
       for (i
@@ -964,24 +964,32 @@
 ) %7B%0A        
+     
+   
 if (firstWor
@@ -1023,24 +1023,34 @@
 harAt(i)) %7B%0A
+          
           co
@@ -1057,32 +1057,40 @@
 ntinue;%0A        
+  
+      
 %7D else %7B%0A       
@@ -1088,24 +1088,34 @@
 %7B%0A          
+     
+     
 if (map.get(
@@ -1166,24 +1166,36 @@
 arAt(i))) %7B%0A
+            
             
@@ -1210,32 +1210,42 @@
 alse;%0A          
+        
+  
 %7D else %7B%0A       
@@ -1241,32 +1241,44 @@
 e %7B%0A            
+           
+ 
 return true;%0A   
@@ -1266,32 +1266,42 @@
    return true;%0A
+          
           %7D%0A    
@@ -1304,24 +1304,46 @@
         
+ 
+       %7D%0A            
 %7D%0A      
 %7D%0A      
@@ -1330,26 +1330,24 @@
     %7D%0A      
-%7D%0A
       return
@@ -1394,16 +1394,22 @@
 ();%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 953