Minimum Deletions to Make String Balanced - Complete Solution Guide
Minimum Deletions to Make String Balanced is LeetCode problem 1653, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 ("aa b abb a b" -> "aaabbb"), or Delete the characters at 0-indexed pos
Detailed Explanation
The problem requires finding the minimum number of character deletions in a string `s` (containing only 'a' and 'b' characters) to make it 'balanced'. A string is balanced if there are no indices `i` and `j` such that `i < j` and `s[i] = 'b'` and `s[j] = 'a'`. In simpler terms, all 'b's must appear before all 'a's, or the string must consist only of 'a's or only of 'b's.
Solution Approach
The solution uses a greedy approach to find the minimum deletions. It iterates through the string, keeping track of the number of 'b's encountered so far (`b_count`). When it encounters an 'a', it calculates the number of deletions needed in two scenarios: 1) delete the current 'a', which would increment `deletions` by 1, or 2) delete all 'b's encountered before this 'a', which is equal to `b_count`. The algorithm chooses the minimum of these two options and updates `deletions` accordingly. If a character is 'b', we simply increment `b_count`. This approach avoids storing the entire string or any large intermediate values, leading to an efficient solution.
Step-by-Step Algorithm
- Step 1: Initialize `deletions` to 0 and `b_count` to 0. These variables will store the minimum number of deletions and the count of 'b's, respectively.
- Step 2: Iterate through the input string `s` character by character.
- Step 3: For each character, check if it is 'a'.
- Step 4: If the character is 'a', update `deletions` to the minimum of `deletions + 1` (deleting the current 'a') and `b_count` (deleting all 'b's encountered before). This chooses the optimal deletion strategy at this point.
- Step 5: If the character is 'b', increment `b_count` by 1.
- Step 6: After iterating through the entire string, return the final value of `deletions`.
Key Insights
- Insight 1: The problem can be solved using dynamic programming or a greedy approach by iterating through the string once.
- Insight 2: For each position in the string, we can choose to either delete the current character, or keep it. Keeping 'a' means deleting all 'b's before it. Keeping 'b' means deleting all 'a's after it. The optimal solution will explore one of these options.
- Insight 3: We can maintain a count of 'b's encountered so far. When we encounter an 'a', we have two choices: either delete the 'a' or delete all the 'b's seen before it. We choose the option that results in fewer deletions.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Dynamic Programming, Stack.
Companies
Asked at: redbus.