1653. Minimum Deletions to Make String Balanced
Question
Code
class Solution:
def minimumDeletions(self, s: str) -> int:
n = len(s)
count_a = [0] * n
a_count = 0
# First pass: compute count_a which stores the number of
# 'a' characters to the right of the current position
for i in range(n - 1, -1, -1):
count_a[i] = a_count
if s[i] == "a":
a_count += 1
min_deletions = n
b_count = 0
# Second pass: compute minimum deletions on the fly
for i in range(n):
min_deletions = min(count_a[i] + b_count, min_deletions)
if s[i] == "b":
b_count += 1
return min_deletions
class Solution:
def minimumDeletions(self, s: str) -> int:
n = len(s)
a_count = sum(1 for ch in s if ch == "a")
b_count = 0
min_deletions = n
# Second pass: iterate through the string to compute minimum deletions
for ch in s:
if ch == "a":
a_count -= 1
min_deletions = min(min_deletions, a_count + b_count)
if ch == "b":
b_count += 1
return min_deletions