# 953.Verifying an Alien Dictionary

**953.Verifying an Alien Dictionary**

难度:Easy

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

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

```
示例 1：

输入：words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
输出：true
解释：在该语言的字母表中，'h' 位于 'l' 之前，所以单词序列是按字典序排列的。
示例 2：

输入：words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
输出：false
解释：在该语言的字母表中，'d' 位于 'l' 之后，那么 words[0] > words[1]，因此单词序列不是按字典序排列的。
示例 3：

输入：words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
输出：false
解释：当前三个字符 "app" 匹配时，第二个字符串相对短一些，然后根据词典编纂规则 "apple" > "app"，因为 'l' > '∅'，其中 '∅' 是空白字符，定义为比任何其他字符都小（更多信息）。
 

提示：

1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
在 words[i] 和 order 中的所有字符都是英文小写字母。
```

建立比较函数。

```
class Solution {
private:
   unordered_map<char,int>ord;
    bool comp(string a, string b)
    {
        int n =min(a.length(),b.length());
        for(int i=0;i<n;i++)
        {
            if(ord[a[i]] <ord[b[i]]) return true;
            else if(ord[a[i]]>ord[b[i]]) return false;
        }
        return a.length() < b.length();
        
        
    }
public:
    bool isAlienSorted(vector<string>& words, string order) {

        int i=0;
        for(char c: order)
            ord[c]=i++;
        for(i=1;i<words.size();i++)
            if(comp(words[i],words[i-1])) return false;
        return true;
    }
};
```

> 执行用时 : 20 ms, 在Verifying an Alien Dictionary的C++提交中击败了40.66% 的用户\
> 内存消耗 : 9.6 MB, 在Verifying an Alien Dictionary的C++提交中击败了70.21% 的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/953.verifying_an_alien_dictionary.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
