-
Notifications
You must be signed in to change notification settings - Fork 0
/
alien_dictionary.cc
40 lines (30 loc) · 1.23 KB
/
alien_dictionary.cc
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
39
40
class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
vector<int> ord(26, 0);
// Get true numbered order of alphabets
for (int i = 0; i < order.size(); i++)
ord[order.at(i) - 'a'] = i;
for (int i = 0; i < words.size() - 1; i++) {
string word1 = words.at(i);
string word2 = words.at(i + 1);
for (int j = 0; j < std::min(word1.size(), word2.size()); j++) {
char c1 = word1.at(j);
char c2 = word2.at(j);
if (c1 != c2) {
if (ord.at(c1 - 'a') > ord.at(c2 - 'a'))
return false;
// continue searching (continue outer for loop)
goto cont;
}
}
// If there was no difference then check if the first word
// is bigger than the second word
if (word1.size() > word2.size())
return false;
// placeholder label to continue outer loop from a nested loop
cont:;
}
return true;
}
};