1653. Minimum Deletions to Make String Balanced LeetCode Solution
In this guide, you will get 1653. Minimum Deletions to Make String Balanced LeetCode Solution with the best time and space complexity. The solution to Minimum Deletions to Make String Balanced problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Deletions to Make String Balanced solution in C++
- Minimum Deletions to Make String Balanced solution in Java
- Minimum Deletions to Make String Balanced solution in Python
- Additional Resources
Problem Statement of Minimum Deletions to Make String Balanced
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'.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1653. Minimum Deletions to Make String Balanced LeetCode Solution in C++
class Solution {
public:
// Same as 926. Flip String to Monotone Increasing
int minimumDeletions(string s) {
// the number of characters to be deleted to make the substring so far
// balanced
int dp = 0;
int countB = 0;
for (const char c : s)
if (c == 'a')
// 1. Delete 'a'.
// 2. Keep 'a' and delete the previous 'b's.
dp = min(dp + 1, countB);
else
++countB;
return dp;
}
};
/* code provided by PROGIEZ */
1653. Minimum Deletions to Make String Balanced LeetCode Solution in Java
class Solution {
// Same as 926. Flip String to Monotone Increasing
public int minimumDeletions(String s) {
// the number of characters to be deleted to make subString so far balanced
int dp = 0;
int countB = 0;
for (final char c : s.toCharArray())
if (c == 'a')
// 1. Delete 'a'.
// 2. Keep 'a' and delete the previous 'b's.
dp = Math.min(dp + 1, countB);
else
++countB;
return dp;
}
}
// code provided by PROGIEZ
1653. Minimum Deletions to Make String Balanced LeetCode Solution in Python
class Solution:
# Same as 926. Flip String to Monotone Increasing
def minimumDeletions(self, s: str) -> int:
dp = 0 # the number of characters to be deleted to make subso far balanced
countB = 0
for c in s:
if c == 'a':
# 1. Delete 'a'.
# 2. Keep 'a' and delete the previous 'b's.
dp = min(dp + 1, countB)
else:
countB += 1
return dp
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.