Check if the Depth of Parentheses is correct in the given String | Function to check if the Depth of Parentheses is correct in the given String ; Appending if the Character is not integer ; Iterating till the entire Digit is read ; Check if character is ' ( ' ; Increment depth by 1 ; Increment open by 1 ; Check if character is ' ) ' ; Decrement depth by 1 ; Increment close by 1 ; Check if open parentheses NOT equals close parentheses ; Driver Code
function Formatted ( s ) { let k = [ ] ; let i = 0 ; while ( i < s . length ) { if ( s [ i ] == ' ' || s [ i ] == ' ' ) { k . push ( s [ i ] ) ; i += 1 ; } else { let st = 0 ; while ( s [ i ] != ' ' && s [ i ] != ' ' ) { st = s [ i ] ; i = i + 1 ; } k . push ( st ) ; } } let depth = 0 , flag = 1 ; let open = 0 , close = 0 ; for ( let i2 = 0 ; i2 < k . length ; i2 ++ ) { if ( k [ i2 ] == ' ' ) { depth += 1 ; open += 1 ; } else if ( k [ i2 ] == ' ' ) { depth -= 1 ; close += 1 ; } else { if ( k [ i2 ] - ' ' != depth ) { flag = 0 ; break ; } } } if ( open != close ) flag = 0 ; return ( flag == 1 ) ? true : false ; } let s = " " ; let k = Formatted ( s ) ; if ( k == true ) document . write ( " " ) ; else document . write ( " " ) ;