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`

**Example 1:**

Input:s = "aababbab"Output:2Explanation:You can either: 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").

**Example 2:**

Input:s = "bbaaaaabb"Output:2Explanation:The only solution is to delete the first two characters.

**Constraints:**

`1 <= s.length <= 10`

^{5}`s[i]`

is`'a'`

or`'b'`

.

```
struct Solution;
impl Solution {
fn minimum_deletions(s: String) -> i32 {
let n = s.len();
let mut a = 0;
let mut b = 0;
for c in s.chars() {
if c == 'a' {
a += 1;
} else {
b += 1;
b = b.max(a + 1);
}
}
(n - a.max(b)) as i32
}
}
#[test]
fn test() {
let s = "aababbab".to_string();
let res = 2;
assert_eq!(Solution::minimum_deletions(s), res);
let s = "bbaaaaabb".to_string();
let res = 2;
assert_eq!(Solution::minimum_deletions(s), res);
}
```