https://www.codewars.com/kata/5277c8a221e209d3f6000b56
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if the string is valid, and false if it's invalid.
This Kata is similar to the Valid Parentheses Kata, but introduces new characters: brackets [], and curly braces {}. Thanks to @arnedag for the idea!
All input strings will be nonempty, and will only consist of parentheses, brackets and curly braces: ()[]{}.
What is considered Valid?
A string of braces is considered valid if all braces are matched with the correct brace.
Examples
"(){}[]" => True
"([{}])" => True
"(}" => False
"[(])" => False
"[({})](]" => False
코드
function validBraces(braces){
let arr = [];
for(let i = 0; i < braces.length; i++){
if(braces[i] === '('){
arr.push(')');
} else if(braces[i] === '['){
arr.push(']');
} else if(braces[i] === '{'){
arr.push('}');
} else{
let brace = arr.pop();
if(brace !== braces[i]){
return false;
}
}
}
return arr.length === 0;
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Snail (4 kyu) / JavaScript (0) | 2023.02.26 |
---|---|
[Codewars] DNA to RNA Conversion (8 kyu) / JavaScript (0) | 2023.02.25 |
[Codewars] Count by X (8 kyu) / JavaScript (0) | 2023.02.23 |
[Codewars] Get the mean of an array (8 kyu) / JavaScript (0) | 2023.02.22 |
[Codewars] Array plus array (8 kyu) / JavaScript (0) | 2023.02.21 |
댓글