<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p>

<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p>

<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aababbab&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or
Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;bbaaaaabb&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li>
</ul>

class Solution:
    def minimumDeletions(self, s: str) -> int:
        # optimized dynamic programming
        

        # dynamic programming
        dp = [0] * (len(s) + 1)
        b_count = 0

        # dp[i]: The number of deletions required to balance the substring s[0, i)
        for i in range(len(s)):
            if s[i] == "b":
                dp[i + 1] = dp[i]
                b_count += 1
            else:
                # Two cases: remove 'a' or keep 'a'
                dp[i + 1] = min(dp[i] + 1, b_count)

        return dp[-1]

        # stack (one pass)
        stack = []
        delete_count = 0

        for c in s:
            if stack and stack[-1] == "b" and c == "a":
                stack.pop()
                delete_count += 1
            else:
                stack.append(c)

        return delete_count

        # two-variable method
        a_count = sum(1 for c in s if c == "a")

        b_count = 0
        min_deletions = float("inf")

        for c in s:
            if c == "a":
                a_count -= 1
            min_deletions = min(min_deletions, a_count + b_count)
            if c == "b":
                b_count += 1

        return min_deletions

        # three-pass count method
        left_b = [0]
        for c in s[:-1]:
            left_b.append(left_b[-1] + c.count("b"))

        right_a = [0]
        for c in s[::-1][:-1]:
            right_a.append(right_a[-1] + c.count("a"))
        right_a = right_a[::-1]

        min_deletions = float("inf")

        for i in range(len(s)):
            min_deletions = min(min_deletions, left_b[i] + right_a[i])

        return min_deletions
        
20118