728x90
๐ 28 - ๋ฌธ์์ด ์์ถ
if-else
๋ฌธ์ ์ด์ฉํด์ ๊ตฌํํ๋ค.
let s = "KKHSSSSSSSE";
console.log(solution(s));
// ๋์์ฝ๋
function solution(s) {
let cnt = 1;
let answer = "";
for (let x of s) {
if (x === answer[answer.length - 1]) cnt++;
else {
if (cnt >= 2) answer += cnt;
answer += x;
cnt = 1;
}
}
return answer;
}
// ๊ฐ์ฌ๋ ์ฝ๋
function solution(s) {
let cnt = 1;
let answer = "";
s += " ";
for (let i=0; i<s.length-1; i++) {
if (s[i] === s[i+1]) cnt++;
else{
answer += s[i];
if(cnt>1) answer+=cnt
cnt = 1
}
}
return answer;
}
๋ฐ์ํ
๋๊ธ