let answer = ''; let base = str[0]; let count = 1; str = str + ' '; for(let i = 1; i<str.length; i++){ if(base === str[i]){ count++; }else{ answer += (count >= 3) ? `${count}${base}` : base.repeat(count); base = str[i]; count = 1; } } return answer;