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:
1 <= s.length <= 105s[i] is 'a' or 'b'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a row of items of two different types, and your goal is to remove some items so that all items of the first type appear before any items of the second type. The brute force strategy involves testing every single spot where you could possibly draw a dividing line and checking how many items you would need to throw away to make everything correct for that specific division.
Here's how the algorithm would work step-by-step:
def minimum_deletions(input_string):
minimum_deletions_found = len(input_string)
# Test every possible dividing wall position from before the first character to after the last
for wall_position in range(len(input_string) + 1):
current_deletions = 0
# Count incorrect 'b' characters on the left side of our imaginary dividing wall
for left_index in range(wall_position):
if input_string[left_index] == 'b':
current_deletions += 1
# Count incorrect 'a' characters on the right side of our imaginary dividing wall
for right_index in range(wall_position, len(input_string)):
if input_string[right_index] == 'a':
current_deletions += 1
# Track the lowest deletion count found across all tested wall positions
if current_deletions < minimum_deletions_found:
minimum_deletions_found = current_deletions
return minimum_deletions_foundTo make the string balanced, we need to ensure that no special first-type character appears after any second-type character. The optimal approach walks through the sequence while keeping a running tally of how many misfits we would need to remove to keep everything in order.
Here's how the algorithm would work step-by-step:
def minimum_deletions(input_string):
# Track invalid 'b' characters that disrupt the balanced order of 'a's and 'b's.
second_type_count = 0
minimum_deletions_needed = 0
for current_character in input_string:
if current_character == 'b':
second_type_count += 1
else:
# Encountering 'a' after 'b' forces a choice to remove one of them.
if second_type_count > 0:
second_type_count -= 1
minimum_deletions_needed += 1
# Reset tally when surplus 'a' characters make prior deletions obsolete.
if minimum_deletions_needed < 0:
minimum_deletions_needed = 0
return minimum_deletions_needed| Case | How to Handle |
|---|---|
| String of length zero or one | The algorithm naturally processes these without error and correctly returns zero since no violations can exist. |
| String already completely balanced | The counting logic will never encounter an 'a' after a 'b', resulting in zero deletions. |
| String with all identical characters such as all 'a's or all 'b's | The algorithm tallies the characters correctly and determines that zero deletions are required. |
| String in completely reverse order such as all 'b's followed by all 'a's | The greedy approach correctly counts the misplaced characters and removes either all 'a's or all 'b's. |
| Extremely large strings up to the maximum constraints | An optimal linear scan approach processes the string in O(n) time and O(1) space, avoiding performance and memory bottlenecks. |
| Alternating characters like abababab | The stack or count tracking efficiently computes the optimal subset of deletions to maintain balance. |