Skip to content

Practice Minimum Deletions to Make String Balanced

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1653
LeetCode Problem

Minimum Deletions to Make String Balanced

Problem Statement

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.

Source: Minimum Deletions to Make String Balanced on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "aababbab"
Output
2

# Example 2

Input
s = "bbaaaaabb"
Output
2

Constraints

1 <= s.length <= 10^5
s[i] is 'a' or 'b'​​.

Related LeetCode Problems

Practice more problems similar to Minimum Deletions to Make String Balanced.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.