shlogg · Early preview
Md Ariful Haque @mah-shamim

Minimum Deletions To Make String Balanced In O(n) Time

Minimum deletions needed to make string balanced: delete max of 'b's before each index and 'a's after it. Time complexity O(n).

1653. Minimum Deletions to Make String Balanced
Medium
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.
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...