953. Verifying an Alien Dictionary
https://leetcode.com/problems/verifying-an-alien-dictionary/
/*
* @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
Last updated