Skip to content

Commit 5bd1454

Browse files
committed
add 0953 folder & cpp(4ms) version
1 parent 94e5ade commit 5bd1454

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
private:
3+
int ala[26] = {0, } ;
4+
public:
5+
bool isAlienSorted(vector<string>& words, string order)
6+
{
7+
if (words.size() <= 1)
8+
return true ;
9+
10+
int i = 0 ;
11+
for (i = 0; i < order.size(); ++i)
12+
ala[order[i] - 'a'] = i ;
13+
14+
for (i = 1; i < words.size(); ++i)
15+
if (!cmp(words[i-1], words[i]))
16+
return false ;
17+
18+
return true ;
19+
}
20+
21+
bool cmp(string a, string b)
22+
{
23+
int i, len = min(a.size(), b.size()) ;
24+
for (i = 0; i < len; ++i)
25+
{
26+
int c = ala[ a[i] - 'a' ] - ala[ b[i] - 'a' ] ;
27+
if (c < 0)
28+
return true ;
29+
else if (c > 0)
30+
return false ;
31+
}
32+
33+
return i == a.size() ;
34+
}
35+
};

0 commit comments

Comments
 (0)