Skip to content

Instantly share code, notes, and snippets.

@MohammedALREAI
Created February 20, 2021 08:05
Show Gist options
  • Save MohammedALREAI/76bfbc30578291727c7b692722af888d to your computer and use it in GitHub Desktop.
Save MohammedALREAI/76bfbc30578291727c7b692722af888d to your computer and use it in GitHub Desktop.
balancedBrackets leetcode
function isValid(s:string ):boolean{
interface matchingBrac{
[key:string]:string}
const open="{[(";
const close="}])"
const matchingBrackets:matchingBrac = {
')': '(',
']': '[',
'}': '{'
};
const stack = [];
for (const char of s) {
if (open.includes(char)) {
stack.push(char);
} else if (close.includes(char)) {
if (!stack.length) {
return false;
}
if (stack[stack.length - 1] === matchingBrackets[char]) {
stack.pop();
} else {
return false;
}
}
}
return stack.length === 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment