forked from deepaktalwardt/interview-prep-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path953-verifying-an-alien-dictionary.cpp
More file actions
36 lines (34 loc) · 1.09 KB
/
953-verifying-an-alien-dictionary.cpp
File metadata and controls
36 lines (34 loc) · 1.09 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
#include <unordered_map>
class Solution {
public:
bool inOrder(unordered_map<char, int>& orderMap, vector<string>& words, int i, int j) {
string word1 = words[i];
string word2 = words[j];
int shorterLen = word1.size() < word2.size() ? word1.size() : word2.size();
for (int k = 0; k < shorterLen; k++) {
char c1 = word1[k];
char c2 = word2[k];
if (c1 == c2) {
continue;
} else if (orderMap[c1] < orderMap[c2]) {
return true;
} else {
return false;
}
}
if (shorterLen == word1.size()) {
return true;
}
return false;
}
bool isAlienSorted(vector<string>& words, string order) {
unordered_map<char, int> orderMap;
for (int i = 0; i < order.size(); i++) {
orderMap[order[i]] = i;
}
for (int i = 1; i < words.size(); i++) {
if (!inOrder(orderMap, words, i - 1, i)) return false;
}
return true;
}
};