Minimize length of a string by removing pairs of consecutive increasing or decreasing digits | Function to find the minimum length of the string possible after removing pairs of consecutive digits ; Initialize the stack st ; Traverse the string S ; If the stack is empty ; Otherwise ; Get the top character of the stack ; If cha and top are consecutive digits ; Otherwise , push the character ch ; Print the size of the stack ; Driver Code
function minLength ( S ) { var st = [ ] ; S . split ( ' ' ) . forEach ( ch => { if ( st . length == 0 ) st . push ( ch ) ; else { var top = st [ st . length - 1 ] ; if ( Math . abs ( ch - top ) == 1 ) st . pop ( ) ; else { st . push ( ch ) ; } } } ) ; return st . length ; } var S = " " ; document . write ( minLength ( S ) ) ;