Created
June 3, 2021 07:46
-
-
Save Ameen-Alam/8ddb96935bf1db22daae1a4d461b1834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function encode(code) { | |
if (!code) return ''; | |
let encode = ''; | |
for (let i = 0; i < code.length; i++) { | |
let count = 1; | |
for (let j = i; j < code.length; j++) { | |
if (code[i] !== code[j+1]) break; | |
count++; | |
i++; | |
} | |
encode += count === 1 ? code[i] : count + code[i]; | |
} | |
return encode | |
} | |
encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"); // "12WB12W3B24WB" | |
encode("AABBBCCCC"); // "2A3B4C" | |
encode(""); // "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.codegrepper.com/code-examples/javascript/run+length+encoding+javascript