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++
#include <bits/stdc++.h>
using namespace std;
int minLength(string str, int len)
{
stack< char > s;
for ( int i = 0; i < len; i++) {
if (s.empty()) {
s.push(str[i]);
}
else {
char c = s.top();
if (c != str[i]
&& toupper (c) == toupper (str[i])) {
s.pop();
}
else {
s.push(str[i]);
}
}
}
return s.size();
}
int main()
{
string str = "ASbBsd" ;
int len = str.length();
cout << minLength(str, len);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minLength(String str, int len)
{
Stack<Character> s = new Stack<Character>();
for ( int i = 0 ; i < len; i++)
{
if (s.empty())
{
s.push(str.charAt(i));
}
else
{
char c = s.peek();
if (c != str.charAt(i) &&
Character.toUpperCase(c) ==
Character.toUpperCase((str.charAt(i))))
{
s.pop();
}
else
{
s.push(str.charAt(i));
}
}
}
return s.size();
}
public static void main(String []args)
{
String str = "ASbBsd" ;
int len = str.length();
System.out.println(minLength(str, len));
}
}
|
Python3
def minLength(string, l) :
s = [];
for i in range (l) :
if ( len (s) = = 0 ) :
s.append(string[i]);
else :
c = s[ - 1 ];
if (c ! = string[i] and
c.upper() = = string[i].upper()) :
s.pop();
else :
s.append(string[i]);
return len (s);
if __name__ = = "__main__" :
string = "ASbBsd" ;
l = len (string);
print (minLength(string, l));
|
C#
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));
}
}
|
Javascript
<script>
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 = "ASbBsd" ;
let len = str.length;
document.write(minLength(str, len));
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(N).