Leetcode Problem 1653. Minimum Deletions to Make String Balanced

1653. Minimum Deletions to Make String Balanced

Leetcode Solutions

Prefix & Suffix Count Approach

  1. Initialize two arrays prefixB and suffixA of the same length as the input string s.
  2. Traverse the string from left to right, counting the number of 'b's encountered so far, and store these counts in prefixB.
  3. Traverse the string from right to left, counting the number of 'a's encountered so far, and store these counts in suffixA.
  4. Initialize a variable minDeletions to a large number.
  5. Iterate through the string, and for each position, calculate the sum of prefixB[i] and suffixA[i]. Update minDeletions with the minimum of its current value and this sum.
  6. Return minDeletions as the result.
UML Thumbnail

Dynamic Programming Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...