Reduce the string to minimum length with the given operation Last Updated : 13 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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.Examples: Input: str = "ASbBsd" Output: 2 Operations 1: "ASbBsd" -> "ASsd" Operations 2: "ASsd" -> "Ad" The string cannot be reduced further.Input: str = "AsSaDda" Output: 1 Operations 1: "AsSaDda" -> "AaDda" Operations 2: "AaDda" -> "Dda" Operations 3: "Dda" -> "a" Approach: 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: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum // possible length str can be reduced // to with the given operation int minLength(string str, int len) { // Stack to store the characters // of the given string stack<char> s; // For every character of the string for (int i = 0; i < len; i++) { // If the stack is empty then push the // current character in the stack if (s.empty()) { s.push(str[i]); } else { // Get the top character char c = s.top(); // If the top element is not equal // to the current element and it // only differs in the case if (c != str[i] && toupper(c) == toupper(str[i])) { // Pop the top element from stack s.pop(); } // Else push the current element else { s.push(str[i]); } } } return s.size(); } // Driver code int main() { string str = "ASbBsd"; int len = str.length(); cout << minLength(str, len); return 0; } Java // Java implementation of the approach import java.util.*; class GFG { // Function to return the minimum // possible length str can be reduced // to with the given operation static int minLength(String str, int len) { // Stack to store the characters // of the given string Stack<Character> s = new Stack<Character>(); // For every character of the string for (int i = 0; i < len; i++) { // If the stack is empty then push the // current character in the stack if (s.empty()) { s.push(str.charAt(i)); } else { // Get the top character char c = s.peek(); // If the top element is not equal // to the current element and it // only differs in the case if (c != str.charAt(i) && Character.toUpperCase(c) == Character.toUpperCase((str.charAt(i)))) { // Pop the top element from stack s.pop(); } // Else push the current element else { s.push(str.charAt(i)); } } } return s.size(); } // Driver code public static void main(String []args) { String str = "ASbBsd"; int len = str.length(); System.out.println(minLength(str, len)); } } // This code is contributed by Rajput-Ji Python3 # Python3 implementation of the approach # Function to return the minimum # possible length str can be reduced # to with the given operation def minLength(string, l) : # Stack to store the characters # of the given string s = []; # For every character of the string for i in range(l) : # If the stack is empty then push the # current character in the stack if (len(s) == 0) : s.append(string[i]); else : # Get the top character c = s[-1]; # If the top element is not equal # to the current element and it # only differs in the case if (c != string[i] and c.upper() == string[i].upper()) : # Pop the top element from stack s.pop(); # Else push the current element else : s.append(string[i]); return len(s); # Driver code if __name__ == "__main__" : string = "ASbBsd"; l = len(string); print(minLength(string, l)); # This code is contributed by AnkitRai01 C# // C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Function to return the minimum // possible length str can be reduced // to with the given operation static int minLength(String str, int len) { // Stack to store the characters // of the given string Stack<char> s = new Stack<char>(); // For every character of the string for (int i = 0; i < len; i++) { // If the stack is empty then push the // current character in the stack if (s.Count==0) { s.Push(str[i]); } else { // Get the top character char c = s.Peek(); // If the top element is not equal // to the current element and it // only differs in the case if (c != str[i] && char.ToUpper(c) == char.ToUpper((str[i]))) { // Pop the top element from stack s.Pop(); } // Else push the current element else { s.Push(str[i]); } } } return s.Count; } // Driver code public static void Main(String []args) { String str = "ASbBsd"; int len = str.Length; Console.WriteLine(minLength(str, len)); } } // This code is contributed by PrinciRaj1992 JavaScript <script> // Javascript implementation of the approach // Function to return the minimum // possible length str can be reduced // to with the given operation function minLength(str, len) { // Stack to store the characters // of the given string let s = []; // For every character of the string for (let i = 0; i < len; i++) { // If the stack is empty then push the // current character in the stack if (s.length==0) { s.push(str[i]); } else { // Get the top character let c = s[s.length - 1]; // If the top element is not equal // to the current element and it // only differs in the case if (c != str[i] && c.toUpperCase() == str[i].toUpperCase()) { // Pop the top element from stack s.pop(); } // Else push the current element else { s.push(str[i]); } } } return s.length; } let str = "ASbBsd"; let len = str.length; document.write(minLength(str, len)); </script> Output: 2 Time Complexity: O(N).Auxiliary Space: O(N). Comment More infoAdvertise with us Next Article Reduce the string to minimum length with the given operation P pulkitjaroli Follow Improve Article Tags : Strings Stack C++ Programs Data Structures DSA +1 More Practice Tags : Data StructuresStackStrings Similar Reads Smallest string which not a subsequence of the given string Given a string str, consisting of lowercase alphabets, the task is to find the shortest string which is not a subsequence of the given string. If multiple strings exist, then print any one of them. Examples: Input: str = "abaabcc" Output: d Explanation: One of the possible shortest string which is n 6 min read Minimum length of Run Length Encoding possible by removing at most K characters from a given string Given a string S of length N, consisting of lowercase English alphabets only, the task is to find the minimum possible length of run-length-encoding that can be generated by removing at most K characters from the string S. Examples: Input: S = "abbbcdcdd", N = 9, K = 2 Output: 5 Explanation: One pos 10 min read Convert given string to another by minimum replacements of subsequences by its smallest character Given two strings A and B, the task is to count the minimum number of operations required to convert the string A to B. In one operation, select a subsequence from string A and convert every character of that subsequence to the smallest character present in it. If it is not possible to transform, th 10 min read 2 Keys Keyboard Problem Given a positive integer N and a string S initially it is "A", the task is to minimize the number of operations required to form a string consisting of N numbers of A's by performing one of the following operations in each step: Copy all the characters present in the string S.Append all the characte 6 min read Find the minimum operations required to type the given String Geek is extremely punctual but today even he is not feeling like doing his homework assignment. He must start doing it immediately in order to meet the deadline. For the assignment, Geek needs to type a string s.To reduce his workload, he has decided to perform one of the following two operations ti 4 min read Like