Created
February 20, 2021 08:05
-
-
Save MohammedALREAI/76bfbc30578291727c7b692722af888d to your computer and use it in GitHub Desktop.
balancedBrackets leetcode
This file contains hidden or 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
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