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
// Valid Braces | |
// https://www.codewars.com/kata/5277c8a221e209d3f6000b56 | |
/* Solution 1 */ | |
function validBraces(braces){ | |
var queue = []; | |
for(var brace of braces) { | |
if(brace === '(' || brace === '{' || brace === '[') { | |
queue.push(brace); | |
} else { | |
var last = queue.pop(); | |
if(last === '(' && brace === ')') { | |
continue; | |
} else if (last === '[' && brace === ']') { | |
continue; | |
} else if (last === '{' && brace === '}') { | |
continue; | |
} else { | |
return false; | |
} | |
} | |
} | |
return queue.length === 0; | |
} | |
/* Solution 2 */ | |
function validBraces(braces){ | |
var matches = { '(':')', '{':'}', '[':']' }; | |
var queue = []; | |
for(var brace of braces) { | |
if (matches[brace]) { | |
queue.push(brace); | |
} else { | |
if (brace !== matches[queue.pop()]) { | |
return false; | |
} | |
} | |
} | |
return queue.length === 0; | |
} | |
/* Solution 3 */ | |
function validBraces(braces){ | |
while(braces.indexOf("{}") != -1 || braces.indexOf("()") != -1 || braces.indexOf("[]") != -1){ | |
braces = braces.replace("{}", "").replace("()", "").replace("[]", ""); | |
} | |
return braces.length == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
que tengo que hacer para conseguir robux