deno.land / x / masx200_leetcode_test@10.6.5 / verifying-an-alien-dictionary / index.ts

نووسراو ببینە
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
export default function isAlienSorted(words: string[], order: string): boolean { const decode_cache = new Map<string, string>(); function decode(word: string): string { const cached = decode_cache.get(word); if (cached) return cached;
const result = String.fromCharCode( ...Array.from(word).map( (char) => "a".charCodeAt(0) + (char_to_index.get(char) ?? -1), ), ); decode_cache.set(word, result); return result; } function checktwo(word1: string, word2: string): boolean { const decoded = [word1, word2].map((w) => decode(w)); const sorted = Array.from(decoded).sort();
return sorted[0] === decoded[0] && decoded[1] === sorted[1]; }
const char_to_index = new Map( [...Array.from(order).entries()].map(([a, b]) => [b, a]), );
return words.every((word, index, array) => { const mext = array[index + 1];
return mext ? checktwo(word, mext) : true; });}
masx200_leetcode_test

Version Info

Tagged at
a year ago