題目網址:http://www.codewars.com/kata/5277c8a221e209d3f6000b56
敘述:
Write a function called validBraces that takes a string of braces, and determines if the order of the braces is valid. validBraces 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 four new characters. Open and closed brackets, and open and closed curly braces. Thanks to @arnedag for the idea!
All input strings will be nonempty, and will only consist of open parentheses '(' , closed parentheses ')', open brackets '[', closed brackets ']', open curly braces '{' and closed curly braces '}'.
What is considered Valid? A string of braces is considered valid if all braces are matched with the correct brace.
For example:
'(){}[]' and '([{}])' would be considered valid, while '(}', '[(])', and '[({})](]' would be considered invalid.
題目網址:http://www.codewars.com/kata/5277c8a221e209d3f6000b56 敘述: Write a function called validBraces that takes a string of braces, and determines if the order of the braces is valid. validBraces 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 four new characters. Open and closed brackets, and open and closed curly braces. Thanks to @arnedag for the idea!
All input strings will be nonempty, and will only consist of open parentheses '(' , closed parentheses ')', open brackets '[', closed brackets ']', open curly braces '{' and closed curly braces '}'.
What is considered Valid? A string of braces is considered valid if all braces are matched with the correct brace. For example: '(){}[]' and '([{}])' would be considered valid, while '(}', '[(])', and '[({})](]' would be considered invalid.
Examples: validBraces( "(){}[]" ) => returns true validBraces( "(}" ) => returns false validBraces( "[(])" ) => returns false validBraces( "([{}])" ) => returns true
翻譯: 寫個函式來測試輸入字串中括號的順序是不是合法的,如果輸入的字串是合法的就回傳
true
,否則回傳false
。 所有的測資都是 nonempty,總共會有六種括號(三種配對) open parentheses(
, closed parentheses)
, open brackets[
, closed brackets]
, open curly braces{
and closed curly braces}
,必須要確定括號們都照順序配對完成,算是合法。validBraces( "(){}[]" ) => returns true validBraces( "(}" ) => returns false (沒有配對完成 validBraces( "[(])" ) => returns false (沒有照順序被配對到 validBraces( "([{}])" ) => returns true