Skip to content

Commit a8e880b

Browse files
authored
20251204 (#31)
- add: 3-5 문자열 압축
1 parent 3d2ca5b commit a8e880b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function solution(str) {
2+
let answer = '';
3+
let count = 1;
4+
str = str + ' '; // 마지막 문자에 대한 처리를 위해 빈 문자 추가 (Sentinel Value)
5+
6+
for (let i = 0; i < str.length; i++) {
7+
const char = str[i];
8+
const nextChar = str[i + 1];
9+
10+
if (char === nextChar) count++;
11+
else {
12+
answer += char;
13+
if (count > 1) answer += String(count);
14+
count = 1;
15+
}
16+
}
17+
18+
return answer;
19+
}
20+
21+
console.log(solution('KKHSSSSSSSE')); // K2HS7E

0 commit comments

Comments
 (0)