We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 8b90d51 + 3d06b13 commit 6aa86e4Copy full SHA for 6aa86e4
javascript/0953-verifying-an-alien-dictionary.js
@@ -0,0 +1,25 @@
1
+var isAlienSorted = function(words, order) {
2
+ // first differing char
3
+ // if word A is prefix of word B, word B must be AFTER word A
4
+ orderInd = new Map(); {
5
+ let ind = 0;
6
+ for(const c of order)
7
+ orderInd.set(c, ind++);
8
+ }
9
+
10
+ for(let i = 0; i < words.length - 1; i++) {
11
+ let w1 = words[i], w2 = words[i + 1];
12
13
+ for(let j = 0; j < w1.length; j++) {
14
+ if(j == w2.length)
15
+ return false;
16
17
+ if(w1.charAt(j) != w2.charAt(j))
18
+ if(orderInd.get(w2.charAt(j)) < orderInd.get(w1.charAt(j)))
19
20
+ else
21
+ break;
22
23
24
+ return true;
25
+};
0 commit comments