-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path953.cpp
More file actions
18 lines (18 loc) · 696 Bytes
/
Copy path953.cpp
File metadata and controls
18 lines (18 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
bool isAlienSorted(vector<string> &words, string order) {
size_t indices[26]{};
for (size_t i = 0; i < order.size(); ++i)
indices[order[i] - 'a'] = i;
return is_sorted(words.begin(), words.end(),
[&indices](const string &w1, const string &w2) {
auto l1 = w1.size(), l2 = w2.size();
for (auto i = 0; i < min(l1, l2); ++i) {
auto c1 = w1[i], c2 = w2[i];
if (c1 != c2)
return indices[c1 - 'a'] < indices[c2 - 'a'];
}
return l1 < l2;
});
}
};