Skip to content

Instantly share code, notes, and snippets.

@shiv19
Created February 11, 2020 01:55
Show Gist options
  • Save shiv19/b066adfa5b9a1c2b123c55af1acca650 to your computer and use it in GitHub Desktop.
Save shiv19/b066adfa5b9a1c2b123c55af1acca650 to your computer and use it in GitHub Desktop.
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