-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-distance.js
More file actions
45 lines (41 loc) ยท 1005 Bytes
/
text-distance.js
File metadata and controls
45 lines (41 loc) ยท 1005 Bytes
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
/**
* @desc problem : ๊ฐ์ฅ ์งง์ ๋ฌธ์๊ฑฐ๋ฆฌ
* @desc site : Olympiad
* @desc level: 3
* @desc solution : ์ ์ํ ํ ์ญ์ํ
*/
/**
* solution
* @param {string} str : ๋์ ๋ฌธ์์ด
* @param {string} target : ํ์ ๋ฌธ์
*/
function solution(str, target) {
const answer = [];
const strArray = str.trim().split('');
let pos = 100;
//์ ์ํ
for (let i = 0; i < strArray.length; i++) {
const char = strArray[i];
if (char === target) {
pos = 0;
} else {
pos++;
}
answer.push(pos);
}
pos = 100;
//์ญ์ํ
for (let j = strArray.length - 1; j >= 0; j--) {
const char = strArray[j];
if (char === target) {
pos = 0;
answer.push(pos);
} else {
pos++;
answer[j] = Math.min(answer[j], pos);
}
}
return answer;
}
const answer = solution('supermario', 'r');
console.log(answer); //4 3 2 1 0 1 1 0 1 2