text: InfyTQ 2019 : Find the position from where the parenthesis is not balanced | Defining the string ; Storing opening braces in list lst1 ; Storing closing braces in list lst2 ; Creating an empty list lst ; Creating dictionary to map closing braces to opening ones ; If first position of string contain any closing braces return 1 ; If characters of string are opening braces then append them in a list ; When size of list is 0 and new closing braces is encountered then print its index starting from 1 ; As we encounter closing braces we map them with theircorresponding opening braces using dictionary and check if it is same as last opened braces ( last element in list ) if yes then we delete that elememt from list ; Otherwise we return the index ( starting from 1 ) at which nesting is found wrong ; At end if the list is empty it means the string is perfectly nested
code: let string = " " ; let lst1 = [ ' ' , ' ' , ' ' ] ; let lst2 = [ ' ' , ' ' , ' ' ] ; let lst = [ ] ; let Dict = { ' ' : ' ' , ' ' : ' ' , ' ' : ' ' } let a = 0 , b = 0 , c = 0 ; if ( string [ 0 ] in lst2 ) { document . write ( 1 + " " ) ; } else { for ( let i = 0 ; i < string . length ; i ++ ) { if ( string [ i ] in lst1 ) { lst . push ( string [ i ] ) ; k = i + 2 ; } else { if ( lst . lengt == 0 && ( string [ i ] in lst2 ) ) { document . write ( ( i + 1 ) + " " ) ; c = 1 ; break ; } else { if ( Dict [ string [ i ] ] == lst [ lst . length - 1 ] ) { lst . pop ( ) ; } else { break ; document . write ( ( i + 1 ) + " " ) ; a = 1 ; } } } } if ( lst . length == 0 && c == 0 ) { document . write ( 0 + " " ) ; b = 1 ; } if ( a == 0 && b == 0 && c == 0 ) { document . write ( k + " " ) ; } }