Created
October 4, 2023 09:23
-
-
Save HashirHussain/1dc32e30e75331df75627d311fef2a66 to your computer and use it in GitHub Desktop.
uncompress bfe.dev solution in javaScript
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 uncompress(str) { | |
var result = str; | |
while (/\d/.test(result)) { | |
var start = result.lastIndexOf("(") | |
var end = result.indexOf(")") | |
var rounds = parseInt(result[start - 1], 10); | |
var repeats = result.substring(start + 1, end).repeat(rounds) | |
result = result.substring(0, start - 1) + repeats + result.substring(end + 1, result.length) | |
} | |
return result; | |
} | |
Test cases | |
var input = "3(ab)" | |
var expectedAnswer = "ababab"; | |
console.log(uncompress(input) === expectedAnswer); | |
var input = "3(ab2(c))" | |
var expectedAnswer = "abccabccabcc"; | |
console.log(uncompress(input) === expectedAnswer); | |
var input = "2(ab2(a2(b)))" | |
var expectedAnswer = "ababbabbababbabb"; | |
console.log(uncompress(input) === expectedAnswer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment