-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-compression.js
More file actions
47 lines (40 loc) · 1023 Bytes
/
string-compression.js
File metadata and controls
47 lines (40 loc) · 1023 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
46
47
/**
* @desc problem : 문자열 압축
* @desc site : Olympiad
* @desc level: 3
* @desc solution : 임시 배열 활용
*/
/**
* solution
* @param {string} str : 대상 문자열
*/
function solution(str) {
const answer = [];
let temp = [];
for (let i = 0; i < str.length; i++) {
const compareChar = temp[0];
const char = str[i];
//비교대상이 없을경우
if (!compareChar) {
temp.push(char);
continue;
}
//비교대상이 달라질 때 (결과 저장)
if (compareChar !== char) {
answer.push(compareChar);
if (temp.length > 1) {
answer.push(temp.length.toString());
}
temp = [];
}
temp.push(char);
}
//나머지 배열 처리
if (temp.length > 1) {
answer.push(temp.length);
}
answer.push(temp.pop());
return answer.join('');
}
const answer = solution('KKHSSSSSSSE');
console.log(answer); //K2HS7E