-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-123-detectDictionary.js
82 lines (66 loc) · 2.13 KB
/
structy-123-detectDictionary.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// https://structy.net/problems/premium/detect-dictionary
// p: arr & str
// r: boolean
const detectDictionary = (dictionary, alphabet) => {
for (let i = 1; i < dictionary.length; i++) {
const word = dictionary[i];
const prevW = dictionary[i - 1];
const maxLen = Math.max(word.length, prevW.length);
for (let j = 0; j < maxLen; j++) {
const indexPrevC = alphabet.indexOf(prevW[j]);
const indexC = alphabet.indexOf(word[j]);
if (indexPrevC > indexC) {
return false;
} else if (indexPrevC < indexC) {
break;
}
}
}
return true;
};
const dictionary = ["zoo", "tick", "tack", "door"];
const alphabet = "ghzstijbacdopnfklmeqrxyuvw";
console.log(detectDictionary(dictionary, alphabet)); // -> true
// z o o
// 2-11-11
// t a c k
// 4-8-9-15
// t i c k
// 4-5-9-15
// door
// const dictionary = ["zoo", "tack", "tick", "door"];
// const alphabet = "ghzstijbacdopnfklmeqrxyuvw";
// console.log(detectDictionary(dictionary, alphabet)); // -> false
//////////////////// not WORKING
// const detectDictionary = (dictionary, alphabet) => {
// let maxLen = -Infinity;
// for (const word of dictionary) {
// maxLen = Math.max(maxLen, word.length);
// }
// const prevW = [];
// const firstW = dictionary[0];
// for (let i = 0; i < firstW.length; i++) {
// const char = firstW[i];
// prevW.push(alphabet.indexOf(char));
// }
// for (let i = 1; i < dictionary.length; i++) {
// const word = dictionary[i];
// for (let j = 0; j < maxLen; j++) {
// const c = word[j];
// const iCurrW = alphabet.indexOf(c);
// const iPrevW = prevW[j];
// if (iPrevW > iCurrW) {
// console.log(word, c, iPrevW, iCurrW);
// return false;
// } else if (iPrevW < iCurrW) {
// console.log(word, c, iPrevW, iCurrW);
// prevW[j] = iCurrW;
// break;
// }
// }
// for (let j = 0; i < word.length; i++) {
// prevW[i] = word[i];
// }
// }
// return true;
// };