[LeetCode] 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 lexicographically 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.

验证外星语字典。

某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。

给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/verifying-an-alien-dictionary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目给了一些单词和一个定好的字典序,请你验证单词是否是有序的。这道题看上去跟经典题 269 很像但是简单很多。思路是把 order 里面的字母之间的顺序转化为 hashmap<letter, order>,然后再对 words 里面的单词进行两两比较。比较的方式是按字母逐个比较,如果同样位置上的两个字母不一样,则判断是否第一个单词的字母的字典序更小,若不是就 return false;如果判断完了并没有找到 false 的情形,则判断是否第一个单词更短,因为除了满足字母之间的顺序之外,字典序也需要满足更短的单词在前。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public boolean isAlienSorted(String[] words, String order) {
 3         HashMap<Character, Integer> map = new HashMap<>();
 4         for (int i = 0; i < order.length(); i++) {
 5             char ch = order.charAt(i);
 6             map.put(ch, i);
 7         }
 8 
 9         for (int i = 0; i < words.length - 1; i++) {
10             if (!inorder(words[i], words[i + 1], map)) {
11                 return false;
12             }
13         }
14         return true;
15     }
16 
17     private boolean inorder(String w1, String w2, HashMap<Character, Integer> map) {
18         for (int i = 0; i < w1.length() && i < w2.length(); i++) {
19             char ch1 = w1.charAt(i);
20             char ch2 = w2.charAt(i);
21             int index1 = map.get(ch1);
22             int index2 = map.get(ch2);
23             if (index1 < index2) {
24                 return true;
25             } else if (index1 > index2) {
26                 return false;
27             }
28         }
29         return w1.length() <= w2.length();
30     }
31 }

 

相关题目

207. Course Schedule

210. Course Schedule II

269. Alien Dictionary

310. Minimum Height Trees

953. Verifying an Alien Dictionary

LeetCode 题目总结

posted @ 2020-05-15 11:56  CNoodle  阅读(243)  评论(0编辑  收藏  举报