Given a string str consisting of lowercase and uppercase characters, the task is to find the minimum possible length the string can be reduced to after performing the given operation any number of times. In a single operation, any two consecutive characters can be removed if they represent the same character in different cases i.e. “aA” and “Cc” can be removed but “cc” and “EE” cannot be removed.
Input: str = “ASbBsd”
Operations 1: “ASbBsd” -> “ASsd”
Operations 2: “ASsd” -> “Ad”
The string cannot be reduced further.
Input: str = “AsSaDda”
Operations 1: “AsSaDda” -> “AaDda”
Operations 2: “AaDda” -> “Dda”
Operations 3: “Dda” -> “a”
- Create a stack to store the characters of the string.
- For every character of the string starting from the first character, if the stack is empty then push the current character in the stack.
- Else match the current character with the top of the stack, if they only differ in the case then pop the element from the stack and continue.
- If they are not equal then push the current element to the stack and repeat the above steps for the rest of the string.
- The size of the stack in the end is the required answer.
Below is the implementation of the above approach:
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum reduce operations to convert a given string into a palindrome
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Transform One String to Another using Minimum Number of Given Operation
- Print the final string when minimum value strings get concatenated in every operation
- Minimum length of string having all permutation of given string.
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Reduce the number to minimum multiple of 4 after removing the digits
- Reduce the string by removing K consecutive identical characters
- Minimum operation require to make first and last character same
- Sub-string that contains all lowercase alphabets after performing the given operation
- Check whether the string S1 can be made equal to S2 with the given operation
- Find length of longest subsequence of one string which is substring of another string
- Check if a string contains a palindromic sub-string of even length
- Minimum length substring with exactly K distinct characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.