#1653
Minimum Deletions to Make String Balanced
pupil · 585 · lc medium +29 · verified · 68.2% accepted · 2,598 likes · top 75%
Description
You are given a string s containing only 'a' and 'b' characters. Remove the minimum number of characters so that no 'b' appears before any 'a' in the resulting string. Return the minimum deletion count.
Example 1:
Input: s = "aababbab"
Output: 2
Explanation: 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: 2
Explanation: The only solution is to delete the first two characters.
Code
1
2
3