Created
February 27, 2017 19:11
-
-
Save Underdoge/ceb02641e57ecdb97883d3d71f6f24cd to your computer and use it in GitHub Desktop.
reverseParentheses
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 flip(flipped){ | |
if(flipped.indexOf('(')>=0){ | |
var j=0; | |
var a=0; | |
var b=0; | |
var reversed=[]; | |
while (j<flipped.length&&b==0) { | |
if(flipped[j]=='('){ | |
a=j; | |
} | |
else | |
if(flipped[j]==')'){ | |
flipped.splice(a,1); | |
flipped.splice(j-1,1); | |
b=j-1; | |
} | |
j++; | |
} | |
reversed=flipped.slice(a,b); | |
reversed.reverse(); | |
for(var i=0;i<reversed.length;i++,a++){ | |
flipped[a]=reversed[i]; | |
} | |
flip(flipped); | |
} | |
return flipped; | |
} | |
function reverseParentheses(s){ | |
var chars=s.split(""); | |
return(flip(chars).join("")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment