- 力扣原题:
- 题目简述:
- 解题思路:
- C++代码:
- 结果展示:
力扣原题:
953. 验证外星语词典
题目简述:某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。
给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。
解题思路:- 使用map得到order的顺序;
- 把目标words转换成我们熟知的英语单词;
- 利用is_sorted函数对我们转换的单词进行顺序判断;
- over;
class Solution {
public:
bool isAlienSorted(vector& words, string order) {
map mapping;
int i = 0;
for(auto ch : order)
{
mapping[ch] = i++;
}
int n = words.size();
vector vec;
for(int i = 0; i < n; i++)
{
string str;
for(auto ch : words[i])
{
str += 'a' + mapping[ch];
}
vec.push_back(str);
}
return is_sorted(vec.begin(), vec.end());
}
};
结果展示:



