-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path953.cpp
More file actions
38 lines (38 loc) · 1.22 KB
/
Copy path953.cpp
File metadata and controls
38 lines (38 loc) · 1.22 KB
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
30
31
32
33
34
35
36
37
38
class Solution {
public:
bool isAlienSorted(vector<string> &words, string order) {
unordered_map<char, int> dic;
dic.insert(pair<char, int>('#', 0));
for (int i = 1; i <= order.size(); ++i)
dic.insert(pair<char, int>(order[i - 1], i));
bool lex = false;
int maxSize = 0;
for (string w : words)
maxSize = maxSize < w.size() ? w.size() : maxSize;
for (int i = 0; i < words.size(); ++i) {
int sub = maxSize - words[i].size();
if (sub > 0)
for (int j = 0; j < sub; ++j)
words[i] += "#";
}
for (int i = 0; i < maxSize; ++i) {
int curLex = 0;
for (int j = 0; j < words.size() - 1; ++j) {
if (dic[words[j][i]] > dic[words[j + 1][i]]) {
curLex = 2;
break;
} else if (dic[words[j][i]] == dic[words[j + 1][i]]) {
curLex = 1;
}
}
if (curLex == 0) {
lex = true;
break;
} else if (curLex == 2)
break;
}
if (lex)
return true;
return false;
}
};