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 <= 105
s[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:
The brute force way to solve this is to try removing every possible combination of letters from the string. For each combination, we check if the resulting string is balanced and keep track of the smallest number of removals it took to achieve that.
Here's how the algorithm would work step-by-step:
def minimum_deletions_to_make_string_balanced_brute_force(input_string):
minimum_deletions = len(input_string)
for i in range(1 << len(input_string)):
current_string = ""
deletions_count = 0
for j in range(len(input_string)):
if (i >> j) & 1:
current_string += input_string[j]
else:
deletions_count += 1
# Check if the current string is balanced
is_balanced = True
b_found = False
for char in current_string:
if char == 'b':
b_found = True
if char == 'a' and b_found:
is_balanced = False
break
#If balanced, check if the number of deletions is minimal
if is_balanced:
minimum_deletions = min(minimum_deletions, deletions_count)
return minimum_deletions
The goal is to find the fewest changes needed to make a string follow the pattern: all 'a's come before all 'b's. We can efficiently determine this by keeping track of the 'a's and 'b's we see as we go through the string, figuring out the best place to split the string into 'a's and 'b's.
Here's how the algorithm would work step-by-step:
def minDeletions(input_string):
left_b_count = 0
right_a_count = input_string.count('a')
minimum_deletions = right_a_count
#Iterate to find optimal split
for index in range(len(input_string)):
if input_string[index] == 'b':
left_b_count += 1
else:
right_a_count -= 1
# Update min deletions needed to balance string
minimum_deletions = min(minimum_deletions, left_b_count + right_a_count)
return minimum_deletions
Case | How to Handle |
---|---|
Null or empty string input | Return 0, as an empty string is already balanced by definition. |
String of length 1 or 2 | Check if the string is already balanced; if not, a single deletion is needed. |
String with all 'a' characters | Return 0, as the string is already balanced. |
String with all 'b' characters | Return 0, as the string is already balanced. |
String with a long sequence of 'b' followed by a long sequence of 'a' | This will likely result in the maximum number of deletions. |
String with alternating 'a' and 'b' characters | A dynamic programming approach calculates the minimum deletions effectively |
Maximum string length (considering memory constraints) | The DP array's size should be carefully considered to avoid exceeding memory limits, possibly requiring optimization techniques. |
Very long string with skewed distribution favoring a's at the beginning and b's at the end | The solution should efficiently handle large input sizes without significant performance degradation. |