File tree Expand file tree Collapse file tree 1 file changed +4
-7
lines changed
src/leet-code/0020-valid-parentheses Expand file tree Collapse file tree 1 file changed +4
-7
lines changed Original file line number Diff line number Diff line change 1
- /** Holds the open-close character relationship between parentheses. */
1
+ // Holds the open-close character relationship between parentheses.
2
2
const parens = new Map ( [
3
3
[ '(' , ')' ] ,
4
4
[ '[' , ']' ] ,
@@ -22,12 +22,9 @@ const hasValidParentheses = (str) => {
22
22
const stack = [ ]
23
23
24
24
for ( const char of str ) {
25
- if ( parens . has ( char ) ) {
26
- const parChar = parens . get ( char )
27
- if ( parChar !== undefined ) stack . push ( parChar )
28
- } else if ( stack . pop ( ) !== char ) {
29
- return false
30
- }
25
+ const closeParen = parens . get ( char )
26
+ if ( closeParen !== undefined ) stack . push ( closeParen )
27
+ else if ( stack . pop ( ) !== char ) return false
31
28
}
32
29
33
30
// If the stack is empty, all parentheses have been closed.
You can’t perform that action at this time.
0 commit comments