Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Decompress A Given String
// 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
You can’t perform that action at this time.