LeetCode 953. Verifying an Alien Dictionary

纵然是瞬间 提交于 2019-12-09 03:45:30

原题链接在这里:https://leetcode.com/problems/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).

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • order.length == 26
  • All characters in words[i] and order are English lowercase letters.

题解:

Given the order in string order, covert it into char with its index relationship.

For current and previous string, check if previous string is bigger than current string. If yes, return false.

In order to check if previous string is bigger than current string, get the corresponding char, when they are not equal, check if previous index is bigger than current one, if yes, return false.

If they keep equal until the end, then check if previous string length > current string length.

Time Complexity: O(nm + k). n = words.length(). m is average length. k = order.length(). k <= 26.

Space: O(1).

AC Java: 

 1 class Solution {
 2     public boolean isAlienSorted(String[] words, String order) {
 3         if(words == null || words.length == 0 || order == null || order.length() == 0){
 4             return true;
 5         }
 6         
 7         int [] map = new int[26];
 8         for(int i = 0; i<order.length(); i++){
 9             map[order.charAt(i)-'a'] = i;
10         }
11         
12         for(int i = 1; i<words.length; i++){
13             if(isBigger(words[i-1], words[i], map)){
14                 return false;
15             }
16         }
17         
18         return true;
19     }
20     
21     private boolean isBigger(String s1, String s2, int [] map){
22         int j = 0;
23         while(j < s1.length() && j < s2.length()){
24             if(s1.charAt(j) != s2.charAt(j)){
25                 return map[s1.charAt(j) - 'a'] > map[s2.charAt(j) - 'a'];
26             }
27             
28             j++;
29         }
30         
31         return s1.length() > s2.length();
32     }
33 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!