Reduce the string to minimum length with the given operation | Function to return the minimum possible length str can be reduced to with the given operation ; Stack to store the characters of the given string ; For every character of the string ; If the stack is empty then push the current character in the stack ; Get the top character ; If the top element is not equal to the current element and it only differs in the case ; Pop the top element from stack ; Else push the current element ;
function minLength ( str , len ) { let s = [ ] ; for ( let i = 0 ; i < len ; i ++ ) { if ( s . length == 0 ) { s . push ( str [ i ] ) ; } else { let c = s [ s . length - 1 ] ; if ( c != str [ i ] && c . toUpperCase ( ) == str [ i ] . toUpperCase ( ) ) { s . pop ( ) ; } else { s . push ( str [ i ] ) ; } } } return s . length ; } ' ' ' ' ' ' let str = " " ; let len = str . length ; document . write ( minLength ( str , len ) ) ;