Minimum Deletions to Make String Balanced

You are given a string s consisting only of characters 'a' and 'b'. You can delete any number of characters from s to make it balanced. A string 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.

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is 'a' or 'b'.
Saving… · 2 lines
class Solution:
def minimumDeletions(self, s: str) -> int:
Console

Click Run to execute sample tests.