> For the complete documentation index, see [llms.txt](https://hao-fu-1.gitbook.io/oj/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hao-fu-1.gitbook.io/oj/search/topological-sort/269.-alien-dictionary/953.-verifying-an-alien-dictionary.md).

# 953. Verifying an Alien Dictionary

给定字符串order表示字符先后顺序，问一组词是否按照order定义的顺序排序。是Alien Dict那道题反过来，思路类似，依次检查相邻的两个word找出第一个不同的字符，然后看是否符合order定义。为了方便查找字符的顺序，用一个map存字符和它的ranking。当没有字符不同时，再检测是否满足长度长的排后面。

```cpp
/*
 * @lc app=leetcode id=953 lang=cpp
 *
 * [953] Verifying an Alien Dictionary
 */

// @lc code=start
class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        unordered_map<char, int> o;
        for (int i = 0; i < order.length(); ++i) {
            o[order[i]] = i;
        }
        for (int i = 1; i < words.size(); ++i) {
            const auto s = words[i - 1], t = words[i];
            bool found = false;
            for (int j = 0; j < min(s.length(), t.length()); ++j) {
                if (s[j] != t[j]) {
                    if (o[s[j]] > o[t[j]]) return false;
                    found = true;
                    break;
                }
            }
            if (!found && s.length() > t.length()) return false; 
        }
        return true;
    }
};
// @lc code=end


```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://hao-fu-1.gitbook.io/oj/search/topological-sort/269.-alien-dictionary/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.
