Reduce the string to minimum length with the given operation | C # implementation of the approach ; 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 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minLength ( String str , int len ) { Stack < char > s = new Stack < char > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( s . Count == 0 ) { s . Push ( str [ i ] ) ; } else { char c = s . Peek ( ) ; if ( c != str [ i ] && char . ToUpper ( c ) == char . ToUpper ( ( str [ i ] ) ) ) { s . Pop ( ) ; } else { s . Push ( str [ i ] ) ; } } } return s . Count ; } public static void Main ( String [ ] args ) { String str = " ASbBsd " ; int len = str . Length ; Console . WriteLine ( minLength ( str , len ) ) ; } }