Skip to content

Commit 281eca1

Browse files
committed
Add 953-VerifyingAnAlienDictionary.cpp
1 parent 5228255 commit 281eca1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
bool cmp(const std::string &x, const std::string &y, const std::vector<int> &vs){
2+
if(x == y){return true;}
3+
for(int p = 0; p < x.size(); p++){
4+
if(p >= y.size()){return false;}
5+
if(vs[x[p] - 'a'] < vs[y[p] - 'a']){return true;}
6+
if(vs[x[p] - 'a'] > vs[y[p] - 'a']){return false;}
7+
8+
}
9+
10+
return true;
11+
}
12+
13+
14+
class Solution {
15+
public:
16+
bool isAlienSorted(vector<string>& words, string order) {
17+
18+
std::vector<int> v(26);
19+
for(int p = 0; p < 26; p++){v[order[p] - 'a'] = p;}
20+
21+
for(int p = 1; p < words.size(); p++){
22+
if(!cmp(words[p - 1], words[p], v)){return false;}
23+
}
24+
25+
return true;
26+
}
27+
};

0 commit comments

Comments
 (0)