The Indian Engineer

Problem 1653 Minimum Deletions to Make String Balanced

Posted on 5 mins

String Dynamic-Programming Stack

Problem Statement

Link - Problem 1653

Question

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 2:

Input: s = "bbaaaaabb"
Output: 2
Explanation: The only solution is to delete the first two characters.

Constraints:

Solution

class Solution {
public:
    int minimumDeletions(string s) {
       int size = s.size();
       int aCnt = 0;
       int bCnt = 0;

       for(auto &ch:s)
        if(ch=='a')
            aCnt++;

        int del = size;

        for(auto &ch:s){
            if(ch=='a')
                aCnt--;
            del = min(del,aCnt+bCnt);
            if(ch=='b')
                bCnt++;
        }

        return del;
    }
};

Complexity Analysis

| Algorithm                | Time Complexity | Space Complexity |
| ------------------------ | --------------- | ---------------- |
| Single-pointer traversal | O(n)            | O(1)             |

Explanation

Intial Thoughts

To approach this problem, we should first understand what makes a string balanced. A string is balanced if there are no pairs of characters where ‘b’ comes before ‘a’. We should look for patterns or sequences in the string that violate this rule. We can think of it like trying to get all the ‘b’s to the right of all the ‘a’s, which can be visualized as a sorting process but with the option to delete characters instead of just rearranging them. This suggests a strategy of tracking the counts of ‘a’ and ‘b’ as we scan through the string and finding the minimum number of deletions needed to achieve balance.

Intuitive Analysis

Intuitively, solving this problem involves scanning the string and keeping track of the counts of ‘a’s and ‘b’s. We should think of it in terms of maintaining a balance between ‘a’s and ‘b’s as we move through the string. The key insight is to realize that the minimum number of deletions will occur at the point where the string becomes balanced with the fewest deletions. We can use variables to keep track of the number of ‘a’s and ‘b’s encountered so far and the minimum number of deletions required up to that point. This approach allows us to methodically consider each character in the string and determine how it affects our goal of achieving a balanced string with the minimum number of deletions.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

You need to find for every index the number of Bs before it and the number of A’s after it

You can speed up the finding of A’s and B’s in suffix and prefix using preprocessing


Similar Questions:

Title URL Difficulty
Check if All A’s Appears Before All B’s https://leetcode.com/problems/check-if-all-as-appears-before-all-bs Easy