DEVDEV

JavaScript Run-length encoding

作成日
2021-10-22
更新日
2021-10-22
js
const getLengthOfOptiomalCompression = function(str) { if (str.length === 0) { console.log('Please enter valid string.'); return; } let output = ''; let count = ''; for (let i = 0; i < str.length; i++) { count++; if (str[i] !== str[i+1]) { output += str[i] + count; count = 0; } } console.log(output); } getLengthOfOptiomalCompression('aaaa'); getLengthOfOptiomalCompression('aaaabbccddd');

Related

TAGS

Algorithm
JavaScript

SHARE