-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0953-verifying-an-alien-dictionary.js
37 lines (33 loc) · 1.12 KB
/
0953-verifying-an-alien-dictionary.js
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
/**
* 953. Verifying an Alien Dictionary
* https://leetcode.com/problems/verifying-an-alien-dictionary/
* Difficulty: Easy
*
* In an alien language, surprisingly, they also use English lowercase letters, but possibly
* in a different order. The order of the alphabet is some permutation of lowercase letters.
*
* Given a sequence of words written in the alien language, and the order of the alphabet,
* return true if and only if the given words are sorted lexicographically in this alien language.
*/
/**
* @param {string[]} words
* @param {string} order
* @return {boolean}
*/
var isAlienSorted = function(words, order) {
const map = new Map(order.split('').map((char, index) => [char, index]));
for (let i = 0; i < words.length - 1; i++) {
if (!helper(words[i], words[i + 1])) return false;
}
return true;
function helper(word1, word2) {
for (let i = 0; i < word1.length; i++) {
if (i >= word2.length) return false;
const char1 = map.get(word1[i]);
const char2 = map.get(word2[i]);
if (char1 < char2) return true;
if (char1 > char2) return false;
}
return true;
}
};