-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0953_isAlienSorted.cc
66 lines (60 loc) · 1.32 KB
/
Problem_0953_isAlienSorted.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
bool isAlienSorted(vector<string> &words, string order)
{
vector<int> idx(26);
for (int i = 0; i < order.length(); i++)
{
// 记录字符序
idx[order[i] - 'a'] = i;
}
for (int i = 1; i < words.size(); i++)
{
bool valid = false;
for (int j = 0; j < words[i - 1].size() && j < words[i].size(); j++)
{
// 比较相邻两个单词的字符序
int pre = idx[words[i - 1][j] - 'a'];
int cur = idx[words[i][j] - 'a'];
if (pre < cur)
{
valid = true;
break;
}
else if (pre > cur)
{
return false;
}
}
if (!valid)
{
if (words[i - 1].size() > words[i].size())
{
return false;
}
}
}
return true;
}
};
void test()
{
Solution s;
vector<string> w1 = {"hello", "leetcode"};
vector<string> w2 = {"word", "world", "row"};
vector<string> w3 = {"apple", "app"};
EXPECT_TRUE(s.isAlienSorted(w1, "hlabcdefgijkmnopqrstuvwxyz"));
EXPECT_FALSE(s.isAlienSorted(w2, "worldabcefghijkmnpqstuvxyz"));
EXPECT_FALSE(s.isAlienSorted(w3, "abcdefghijklmnopqrstuvwxyz"));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}