Minimum deletions to make string balanced

August 22, 2022

dynamic-programming

Problem URL: Minimum deletions to make string balanced

We can count the number of occurance of b and then reduce the count with an upcoming a character, also increase the result count on the way. Finally return our result count after the iteration is done.

class Solution:
    def minimumDeletions(self, s: str) -> int:
        bCount = 0
        res = 0
        for char in s:
            if char == "b":
                bCount += 1
            else:
                if bCount > 0:
                    res += 1
                    bCount -= 1
        return res

Time Complexity: O(n)
Space Complexity: O(1)