> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/string/medium/1653.-minimum-deletions-to-make-string-balanced.md).

# 1653. Minimum Deletions to Make String Balanced

## Problem Statement

<br>

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***.

&#x20;

**Example 1:**

<pre><code><strong>Input: s = "aababbab"
</strong><strong>Output: 2
</strong><strong>Explanation: You can either:
</strong>Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
</code></pre>

**Example 2:**

<pre><code><strong>Input: s = "bbaaaaabb"
</strong><strong>Output: 2
</strong><strong>Explanation: The only solution is to delete the first two characters.
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= s.length <= 105`
* `s[i]` is `'a'` or `'b'`​​.

## Intuition

```
Take count of all Those before B's and All after a's for all index

Now the addition for each index tells me the deletion I want to do 
To eliminiate the inversions
```

### Links

<https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/description/?envType=daily-question&envId=2024-07-30>

### Video Links

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int minimumDeletions(string s) {
        int n = s.size();
        vector<int> b(n, 0);

        for(int i=1; i<n; i++){
            if(s[i-1] == 'b'){
                b[i] = b[i-1] + 1;
            }else{
                b[i] = b[i-1];
            }
        }


        int ans = INT_MAX;
        for(int i=0; i<n; i++){
            ans = min(ans, a[i]+b[i]);
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/string/medium/1653.-minimum-deletions-to-make-string-balanced.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
