Created
February 11, 2020 01:55
-
-
Save shiv19/b066adfa5b9a1c2b123c55af1acca650 to your computer and use it in GitHub Desktop.
Decompress A Given String
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
// An alternate solution for this challenge | |
// https://www.adimation.info/2019/12/01/decompress-a-given-string | |
function decodeString(str) { | |
let res = ''; | |
const letters = str.split(/[0-9]+/).join('').split(''); | |
letters.forEach(v => { | |
if (str.match(new RegExp(`${v}[0-9]+`))) { | |
res += v.repeat(str.match(new RegExp(`${v}[0-9]+`))[0].split(v)[1]); | |
} else { | |
res += v; | |
} | |
}) | |
return res; | |
} | |
console.log(decodeString('a2b2c2')); // aabbcc | |
console.log(decodeString('a3bc4d10')); // aaabccccdddddddddd | |
console.log(decodeString('a0b0c0dd')); // dd | |
console.log(decodeString('A10')); // AAAAAAAAAA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment