解题思路:
(1)使用hash map存储排序索引
(2)依次比较相邻字符串的大小
class Solution {
private:
unordered_map<char,int> mp;
public:
bool Judge(string &str1,string &str2) {
int i=0,j=0;
while(i<str1.length() && j<str2.length()) {
if(mp[str1[i]]<mp[str2[j]]) return true;
else if(mp[str1[i]]>mp[str2[j]]) return false;
else {
i++;
j++;
}
}
if(i!=str1.length()) return false;
else if(j!=str2.length()) return true;
return true;
}
bool isAlienSorted(vector<string>& words, string order) {
if(words.size()==1) return true;
for(int i=0;i<order.length();i++) {
mp[order[i]]=i;
}
for(int i=0;i<words.size()-1;i++) {
if(!Judge(words[i],words[i+1])) return false;
}
return true;
}
};