1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
var compress = function (chars) { let count;
for (let i = 0; i < chars.length - 1; i++) { if (chars[i + 1] === chars[i]) { count = 1;
for (let j = i + 1; j < chars.length; j++) { if (chars[j] !== chars[i]) { break; } count++; }
count !== 1 && chars.splice(i + 1, count - 1, ...('' + count));
i++; } }
return chars.length; };
|