forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
29 lines (28 loc) · 749 Bytes
/
solution.cpp
File metadata and controls
29 lines (28 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
bool less(string &s1, string &s2, map<char, int> &m){
int l = min(s1.size(),s2.size());
for(int i=0;i<l;i++){
if(m[s1[i]]<m[s2[i]])
return true;
if(m[s1[i]]>m[s2[i]])
return false;
}
if(s1.size() <= s2.size())
return true;
return false;
}
bool isAlienSorted(vector<string>& words, string order) {
map<char, int> m;
int i=0;
for(char &a:order){
m.insert(pair<char, int>(a,i));
i++;
}
for(int i=1;i<words.size();i++){
if(!less(words[i-1],words[i],m))
return false;
}
return true;
}
};