-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript.js
31 lines (31 loc) · 955 Bytes
/
Javascript.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
let listWord = [];
const N = parseInt(readline());
for (let i = 0; i < N; i++) {
listWord.push(readline());
} // Fill listWord
let sortedList = [...listWord].sort(); // Creates a copy of listWord sort by alphabetical order
let listPrefix = [];
for (let i = 0; i < N; i++) {
let el = sortedList[i];
let el1 = i < N - 1 ? sortedList[i + 1] : null;
let el2 = i > 0 ? sortedList[i - 1] : null;
let j = 0;
while (el1 != null || el2 != null) {
//count the imilarities between the -1 and +1 element
if (el1 != null) {
if (el1[j] != el[j]) {
el1 = null;
} //If there is a difference set the element to null
}
if (el2 != null) {
if (el2[j] != el[j]) {
el2 = null;
} //If there is a difference set the element to null
}
j++;
}
listPrefix.push(sortedList[i].substring(0, j)); //Create Prefix
}
for (let i = 0; i < N; i++) {
console.log(listPrefix[sortedList.indexOf(listWord[i])]);
}