Minimum String Length After Removing Substrings

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
You are given a string s consisting only of uppercase English letters.
You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings "AB" or "CD" from s.
Return the minimum possible length of the resulting string that you can obtain.
Note that the string concatenates after removing the substring and could produce new "AB" or "CD" substrings.
LeetCode Problem - 2696
class Solution {
public int minLength(String s) {
// Initialize a stack to store characters
Stack<Character> stack = new Stack<>();
// Iterate over each character in the string
for (int i = 0; i < s.length(); i++) {
char currentChar = s.charAt(i); // Get the current character
// If the stack is empty, push the current character
if (stack.isEmpty()) {
stack.push(currentChar);
}
// If the current character is 'B' and the top of the stack is 'A', remove 'A'
else if (currentChar == 'B' && stack.peek() == 'A') {
stack.pop();
}
// If the current character is 'D' and the top of the stack is 'C', remove 'C'
else if (currentChar == 'D' && stack.peek() == 'C') {
stack.pop();
}
// Otherwise, push the current character onto the stack
else {
stack.push(currentChar);
}
}
// Return the size of the stack, which represents the minimum length of the string
return stack.size();
}
}




