Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum deletions to make string balanced in Python
Suppose we have a string s with only two characters 's' and 't'. We can delete any number of characters of s to make the string balanced. We can say, s is balanced when there is no pair of indices (i,j) such that i < j and s[i] = 't' and s[j]= 's'. We have to find the minimum number of deletions needed to make s balanced.
So, if the input is like s = "sststtst", then the output will be 2 because we can either remove the characters at indices 2 and 6 ("sststtst" to "sssttt"), or remove the characters at indices 3 and 6 ("sststtst" to "sstttt").
Algorithm
To solve this, we will follow these steps −
cum_b := 0 (count of 't' characters seen so far)
count_a := number of character 's' in s
ans := infinity
-
for each x in s, do
-
if x is same as "s", then
count_a := count_a - 1
ans := minimum of ans and (cum_b + count_a)
-
otherwise,
cum_b := cum_b + 1
ans := minimum of ans and (cum_b-1 + count_a)
-
return ans
How It Works
The algorithm works by considering each position as a potential split point. At each position, we can either delete all 't' characters seen so far or delete all remaining 's' characters. The minimum of these options gives us the optimal deletions.
Example
Let us see the following implementation to get better understanding −
def solve(s):
cum_b = 0
count_a = s.count("s")
ans = float("inf")
for x in s:
if x == "s":
count_a -= 1
ans = min(ans, cum_b + count_a)
else:
cum_b += 1
ans = min(ans, cum_b - 1 + count_a)
return ans
s = "sststtst"
print(solve(s))
The output of the above code is −
2
Step-by-Step Trace
For string "sststtst" −
def solve_with_trace(s):
cum_b = 0
count_a = s.count("s")
ans = float("inf")
print(f"Initial: count_a = {count_a}")
for i, x in enumerate(s):
if x == "s":
count_a -= 1
deletions = cum_b + count_a
ans = min(ans, deletions)
print(f"Position {i}: '{x}' - deletions = {cum_b} + {count_a} = {deletions}")
else:
cum_b += 1
deletions = cum_b - 1 + count_a
ans = min(ans, deletions)
print(f"Position {i}: '{x}' - deletions = {cum_b-1} + {count_a} = {deletions}")
return ans
s = "sststtst"
result = solve_with_trace(s)
print(f"Minimum deletions: {result}")
Initial: count_a = 4 Position 0: 's' - deletions = 0 + 3 = 3 Position 1: 's' - deletions = 0 + 2 = 2 Position 2: 't' - deletions = 0 + 2 = 2 Position 3: 's' - deletions = 1 + 1 = 2 Position 4: 't' - deletions = 1 + 1 = 2 Position 5: 't' - deletions = 2 + 1 = 3 Position 6: 's' - deletions = 3 + 0 = 3 Position 7: 't' - deletions = 3 + 0 = 3 Minimum deletions: 2
Conclusion
This algorithm efficiently finds the minimum deletions needed to balance a string by considering each position as a potential split point. The time complexity is O(n) and space complexity is O(1), making it an optimal solution.
