Skip to content

Commit ad8872f

Browse files
committed
Time: 18 ms (5.61%), Space: 12.5 MB (5.21%) - LeetHub
1 parent 1fcbf51 commit ad8872f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
unordered_map<char, int> m;
2+
3+
bool cmp(string a, string b){
4+
int n = min(a.length(), b.length());
5+
for(int i = 0; i < n; i++){
6+
if(m[a[i]] < m[b[i]]){
7+
return true;
8+
}
9+
else if(m[a[i]] > m[b[i]]){
10+
return false;
11+
}
12+
}
13+
if(a.length() <= b.length()){
14+
return true;
15+
}
16+
return false;
17+
}
18+
19+
class Solution {
20+
public:
21+
bool isAlienSorted(vector<string>& words, string order) {
22+
for(int i = 0; i < order.length(); i++){
23+
m[order[i]] = i;
24+
}
25+
vector<string> temp = words;
26+
sort(temp.begin(), temp.end(), cmp);
27+
return temp == words;
28+
}
29+
};

0 commit comments

Comments
 (0)