Created
May 18, 2018 21:24
-
-
Save vampaynani/110b17a944259f95e0c1f0c62d82df04 to your computer and use it in GitHub Desktop.
Matching parentheses in an expression recognizing three pairs of brackets: (), [], and {}
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 match(str){ | |
| var temp = []; | |
| for(var i = 0; i < str.length; i++){ | |
| var current = str[i]; | |
| var lastChild = temp[temp.length - 1]; | |
| console.log(current, lastChild); | |
| if(current === '(' || current === '{' || current === '['){ | |
| temp.push(current); | |
| } | |
| if(temp.length > 0){ | |
| if(current === ')' && lastChild === '('){ | |
| temp.pop(); | |
| } | |
| if(current === ']' && lastChild === '['){ | |
| temp.pop(); | |
| } | |
| if(current === '}' && lastChild === '{'){ | |
| temp.pop(); | |
| } | |
| } else { | |
| if(current === ')' || current === '}' || current === ']'){ | |
| temp.push(current); | |
| } | |
| } | |
| console.log(temp); | |
| } | |
| return temp <= 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment