-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalien_dictionary.cpp
50 lines (37 loc) · 1.51 KB
/
alien_dictionary.cpp
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
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
//array<int,26> charOrder;
int charOrder[26];
// making ordering array for the order sequence
for(int i=0;i<26;i++)
{
charOrder[order[i]-'a'] = i;
}
for(int i=0;i<words.size()-1;i++)
{
string currentWord = words[i];
string nextWord = words[i+1];
int len = min(currentWord.size(),nextWord.size());
for(int j=0;j<len;j++)
{
if(charOrder[currentWord[j]-'a'] < charOrder[nextWord[j]-'a'])
{
std::cout<< " curr: "<<currentWord[j] <<" next: "<<nextWord[j]<<std::endl;
break;
}
else if (charOrder[currentWord[j]-'a'] > charOrder[nextWord[j]-'a'])
{
std::cout<< " curr greater: "<<currentWord[j] <<" next: "<<nextWord[j]<<std::endl;
return false;
}
else if(j == len-1 && nextWord.size() < currentWord.size())
{
std::cout<< " curr greter in len: "<<currentWord[j] <<" next: "<<nextWord[j]<<std::endl;
return false;
}
}
}
return true;
}
};