Skip to content

Commit 59059ea

Browse files
authored
Create 953-verifying-an-alien-dictionary.js
1 parent 11f1b4e commit 59059ea

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

953-verifying-an-alien-dictionary.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {string} order
4+
* @return {boolean}
5+
*/
6+
const isAlienSorted = function(words, order) {
7+
const mapping = Array(26).fill(0), a = 'a'.charCodeAt(0)
8+
for(let i = 0, len = order.length; i < len; i++) {
9+
mapping[order.charCodeAt(i) - a] = i
10+
}
11+
12+
for(let i = 1, n = words.length; i < n; i++) {
13+
if(bigger(words[i - 1], words[i])) return false
14+
}
15+
16+
return true
17+
18+
function bigger(s1, s2) {
19+
const n = s1.length, m = s2.length;
20+
for (let i = 0; i < n && i < m; ++i) {
21+
if (s1.charAt(i) != s2.charAt(i)) return mapping[s1.charCodeAt(i) - a] > mapping[s2.charCodeAt(i) - a];
22+
}
23+
24+
return n > m;
25+
}
26+
27+
};

0 commit comments

Comments
 (0)