Created
October 3, 2017 20:51
-
-
Save unilecs/98b044a5ca7fa8153f5cbf0c4258451d to your computer and use it in GitHub Desktop.
Compress string: ABBCCCD -> AB2C3D
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 compressString(str) { | |
// regexp | |
const zip = str.replace(/(.)\1{0,}/g, | |
group => (group[0] + (group.length > 1 ? group.length : ''))); | |
return zip; | |
} | |
const str = 'AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD'; | |
console.info("Compression of string: ", compressString(str)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment