-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0269_alienDictionary.js
60 lines (50 loc) · 1.6 KB
/
0269_alienDictionary.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @param {string[]} words Array of strings.
* @return {string} String containing letters in order for alien language.
* @summary Alien Dictionary {@link https://leetcode.com/problems/alien-dictionary/}
* @description Given a list of words in alien dictionary which are sorted by rules of the language, return the order of letters in the language.
* Space O(1) - store number of relations and at mosat unique records for number of unique chars.
* Time O(n) - where n is the length of all of the words combined.
*/
const alienOrder = words => {
const adjList = {};
const counts = {};
for (let i = 0; i < words.length; i++) {
const word = words[i];
for (let j = 0; j < word.length; j++) {
const char = word[j];
adjList[char] = [];
counts[char] = 0;
}
}
for (let i = 0; i < words.length - 1; i++) {
const w1 = words[i];
const w2 = words[i + 1];
if (w1.length > w2.length && !w1.indexOf(w2)) {
return '';
}
for (let j = 0; j < Math.min(w1.length, w2.length); j++) {
const c1 = w1[j];
const c2 = w2[j];
if (c1 !== c2) {
adjList[w1[j]].push(w2[j]);
counts[w2[j]]++;
break;
}
}
}
const answer = [];
const queue = [];
Object.keys(counts).forEach(char => {
if (!counts[char]) queue.push(char);
});
while (queue.length) {
const char = queue.pop();
answer.push(char);
(adjList[char] || []).forEach(adjacent => {
counts[adjacent]--;
if (!counts[adjacent]) queue.unshift(adjacent);
});
}
return answer.length < Object.keys(counts).length ? '' : answer.join('');
};