Skip to content

Instantly share code, notes, and snippets.

@ikyamelia
Created August 12, 2021 05:06
Show Gist options
  • Save ikyamelia/e4409b33c1c9c52bce282a57bc5991f2 to your computer and use it in GitHub Desktop.
Save ikyamelia/e4409b33c1c9c52bce282a57bc5991f2 to your computer and use it in GitHub Desktop.
Tech Tryout Glints - 03
function BracketMatcher(str) {
var lP = 0;
var rP = 0;
for (var i=0; i<str.length; i++) {
if(str[i] === '(') lP++;
if(str[i] === ')') rP++;
if(rP > lP) return 0;
}
if (rP === lP) return 1;
return 0;
}
// keep this function call here
console.log(BracketMatcher(readline()));

Bracket Matcher

Have the function BracketMatcher(str) take the str parameter being passed and return 1 if the brackets are correctly matched and each one is accounted for. Otherwise return 0. For example: if str is "(hello (world))", then the output should be 1, but if str is "((hello (world))" the the output should be 0 because the brackets do not correctly match up. Only "(" and ")" will be used as brackets. If str contains no brackets return 1.

Examples

Input: "(coder)(byte))"
Output: 0
Input: "(c(oder)) b(yte)"
Output: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment