-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-122-lexicalOrder.js
40 lines (32 loc) · 1.12 KB
/
structy-122-lexicalOrder.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
// p: 3 strings
// r: boolean
const lexicalOrder = (word1, word2, alphabet) => {
const len = Math.max(word1.length, word2.length);
for (let i = 0; i < len; i++) {
const c1 = word1[i];
const c2 = word2[i];
if (alphabet.indexOf(c1) > alphabet.indexOf(c2)) {
return false;
} else if (alphabet.indexOf(c1) < alphabet.indexOf(c2)) {
return true;
}
}
return true;
};
// const lexicalOrder = (word1, word2, alphabet) => {
// const len = word1.length < word2.length ? word1.length : word2.length;
// for (let i = 0; i < len; i++) {
// const c1 = word1[i];
// const c2 = word2[i];
// console.log(c1, alphabet.indexOf(c1), c2, alphabet.indexOf(c2));
// if (alphabet.indexOf(c1) === alphabet.indexOf(c2)) {
// continue;
// } else if (alphabet.indexOf(c1) > alphabet.indexOf(c2)) {
// return false;
// } else return true;
// }
// return true;
// };
const alphabet = "abcdefghijklmnopqrstuvwxyz";
console.log(lexicalOrder("apple", "dock", alphabet)); // -> true
console.log(lexicalOrder("apple", "ample", alphabet)); // -> false