diff --git "a/src/inflearn_coding_test/basic/3-5_\353\254\270\354\236\220\354\227\264\354\225\225\354\266\225.js" "b/src/inflearn_coding_test/basic/3-5_\353\254\270\354\236\220\354\227\264\354\225\225\354\266\225.js" new file mode 100644 index 0000000..c6a92f1 --- /dev/null +++ "b/src/inflearn_coding_test/basic/3-5_\353\254\270\354\236\220\354\227\264\354\225\225\354\266\225.js" @@ -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