Minimum Deletions to Make String Balanced

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 characters 'a' and 'b'.
You can delete any number of characters in s to make s balanced. s is balanced if there is no pair of indices (i,j) such that i < j and s[i] = 'b' and s[j]= 'a'.
Return the minimum number of deletions needed to make s balanced.
LeetCode Problem - 1653
class Solution {
public int minimumDeletions(String s) {
int result = 0; // Initialize the result counter to keep track of minimum deletions
Stack<Character> stack = new Stack<>(); // Create a stack to manage characters
// Iterate through each character in the string
for (int i = 0; i < s.length(); i++) {
// If the character is 'b', push it onto the stack
if (s.charAt(i) == 'b') {
stack.push(s.charAt(i));
}
// If the character is 'a'
else if (s.charAt(i) == 'a') {
// Check if the stack is not empty
if (!stack.isEmpty()) {
// Pop the top character from the stack
stack.pop();
// Increment the result counter as a deletion is needed
result++;
}
}
}
// Return the total number of deletions required
return result;
}
}




