Skip to content

1653. Minimum Deletions to Make String Balanced #138

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

To solve this problem, we can follow these steps:

  1. Preprocess the counts of 'b's up to each index: This helps in knowing how many 'b's are present before any given index.
  2. Preprocess the counts of 'a's from each index: This helps in knowing how many 'a's are present after any given index.
  3. Calculate the minimum deletions needed: For each index, you can calculate the deletions needed by considering deleting all 'b's before it and all 'a's after it.

Let's implement this solution in PHP: 1653. Minimum Deletions to Make String Balanced

<?php
// Test cases
echo minimumDeletions("aababbab") . "\n"; // Output: 2
echo minimumDeletions("bbaaaaabb") . "\n"; // Output: 2
?>

Explanation:

  1. Prefix Array…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
1 participant