Skip to content

Commit 7061201

Browse files
committedMar 28, 2025
Add solution #953
1 parent 5dff3d7 commit 7061201

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,038 LeetCode solutions in JavaScript
1+
# 1,039 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -762,6 +762,7 @@
762762
950|[Reveal Cards In Increasing Order](./solutions/0950-reveal-cards-in-increasing-order.js)|Medium|
763763
951|[Flip Equivalent Binary Trees](./solutions/0951-flip-equivalent-binary-trees.js)|Medium|
764764
952|[Largest Component Size by Common Factor](./solutions/0952-largest-component-size-by-common-factor.js)|Hard|
765+
953|[Verifying an Alien Dictionary](./solutions/0953-verifying-an-alien-dictionary.js)|Easy|
765766
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
766767
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
767768
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 953. Verifying an Alien Dictionary
3+
* https://leetcode.com/problems/verifying-an-alien-dictionary/
4+
* Difficulty: Easy
5+
*
6+
* In an alien language, surprisingly, they also use English lowercase letters, but possibly
7+
* in a different order. The order of the alphabet is some permutation of lowercase letters.
8+
*
9+
* Given a sequence of words written in the alien language, and the order of the alphabet,
10+
* return true if and only if the given words are sorted lexicographically in this alien language.
11+
*/
12+
13+
/**
14+
* @param {string[]} words
15+
* @param {string} order
16+
* @return {boolean}
17+
*/
18+
var isAlienSorted = function(words, order) {
19+
const map = new Map(order.split('').map((char, index) => [char, index]));
20+
21+
for (let i = 0; i < words.length - 1; i++) {
22+
if (!helper(words[i], words[i + 1])) return false;
23+
}
24+
25+
return true;
26+
27+
function helper(word1, word2) {
28+
for (let i = 0; i < word1.length; i++) {
29+
if (i >= word2.length) return false;
30+
const char1 = map.get(word1[i]);
31+
const char2 = map.get(word2[i]);
32+
if (char1 < char2) return true;
33+
if (char1 > char2) return false;
34+
}
35+
return true;
36+
}
37+
};

0 commit comments

Comments
 (0)
Please sign in to comment.