Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/inflearn_coding_test/basic/3-5_문자열압축.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function solution(str) {
let answer = '';
let count = 1;
str = str + ' '; // 마지막 문자에 대한 처리를 위해 빈 문자 추가 (Sentinel Value)

for (let i = 0; i < str.length; i++) {
const char = str[i];
const nextChar = str[i + 1];

if (char === nextChar) count++;
else {
answer += char;
if (count > 1) answer += String(count);
count = 1;
}
}

return answer;
}

console.log(solution('KKHSSSSSSSE')); // K2HS7E