Skip to content

Comments

Implement minimum-deletions-to-make-string-balanced#8

Merged
yamcodes merged 4 commits intomainfrom
minimum-deletions-to-make-string-balanced
Feb 8, 2026
Merged

Implement minimum-deletions-to-make-string-balanced#8
yamcodes merged 4 commits intomainfrom
minimum-deletions-to-make-string-balanced

Conversation

@yamcodes
Copy link
Owner

@yamcodes yamcodes commented Feb 8, 2026

Implement minimum-deletions-to-make-string-balanced

Summary by CodeRabbit

Release Notes

  • New Features

    • Added fully-functional algorithm implementation with intelligent cost evaluation mechanism that analyzes multiple partitioning points to determine the optimal solution.
  • Tests

    • Substantially expanded test suite with additional comprehensive test cases covering various input patterns and boundary conditions for robust validation.

@coderabbitai
Copy link

coderabbitai bot commented Feb 8, 2026

Warning

Rate limit exceeded

@yamcodes has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 19 minutes and 11 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Walkthrough

This PR implements the core algorithm for calculating minimum character deletions to balance a string of 'a' and 'b' characters. A new helper method counts 'b' characters before and 'a' characters after each position, enabling the main method to compute the minimum deletion count across all possible split points. Test coverage expands with five additional test cases.

Changes

Cohort / File(s) Summary
Algorithm Implementation
src/main/java/.../minimumdeletionstomakestringbalanced/Solution.java
Added getSliceInfo(String, int) helper method returning counts of 'b' before and 'a' after a given index. Replaced placeholder logic in minimumDeletions() with full O(n²) scan that iterates over split indices, retrieves info from helper, and tracks minimum deletion cost.
Test Cases & Imports
src/test/java/.../minimumdeletionstomakestringbalanced/SolutionTest.java
Reordered imports; expanded test data with five new test cases (b→0, ba→1, aabbbaa→3, abababab→3, bbbbaaaa→0) alongside existing cases.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A bunny hops through strings of 'a' and 'b',
Counting left and right with glee,
Balancing characters, slice by slice,
Finding deletions? Oh how nice!
Now tests validate our logic tight,
The algorithm shines so bright! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main change: implementing the minimum-deletions-to-make-string-balanced solution.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch minimum-deletions-to-make-string-balanced

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In
`@src/main/java/codes/yam/leetcode/minimumdeletionstomakestringbalanced/Solution.java`:
- Around line 5-24: Add HTML-formatted Javadoc to both methods: document the
public method minimumDeletions(String s) with a descriptive summary and include
"Time Complexity: <code>...</code>" and "Space Complexity: <code>...</code>"
tags, and update getSliceInfo(String s, int index)'s Javadoc to use the same
HTML formatting (summary, param tags, return tag) plus explicit Time Complexity
and Space Complexity in <code>...</code> tags; ensure the param names and return
description reference getSliceInfo and minimumDeletions exactly as named so
reviewers can find the methods.

In
`@src/test/java/codes/yam/leetcode/minimumdeletionstomakestringbalanced/SolutionTest.java`:
- Around line 14-21: Update the incorrect expected values in the parameterized
test data: in the Stream.of arguments for the test method (the arguments(...)
entries), change the expected value for the input "aabbbaa" from 3 to 2 and
change the expected value for "bbbbaaaa" from 0 to 4 so the assertions use the
correct minimal deletion counts.
🧹 Nitpick comments (1)
src/main/java/codes/yam/leetcode/minimumdeletionstomakestringbalanced/Solution.java (1)

26-34: Consider an O(n) single-pass approach instead of O(n²).

The current implementation calls getSliceInfo (an O(n) scan) for every index, yielding O(n²) overall. This will TLE on LeetCode's larger inputs (n up to 10⁵). A single-pass O(n) solution is straightforward: track a running count of 'b's seen so far, and at each 'a', decide whether to "delete" it or pair it against a previous 'b'.

♻️ O(n) implementation
  int minimumDeletions(String s) {
-   int min = Integer.MAX_VALUE;
-   for (int i = 0; i < s.length(); i++) {
-     int[] info = getSliceInfo(s, i);
-     int computed = info[0] + info[1];
-     min = Math.min(min, computed);
-   }
-   return min;
+   int deletions = 0;
+   int bCount = 0;
+   for (int i = 0; i < s.length(); i++) {
+     if (s.charAt(i) == 'b') {
+       bCount++;
+     } else if (bCount > 0) {
+       deletions++;
+       bCount--;
+     }
+   }
+   return deletions;
  }

@yamcodes yamcodes merged commit c2e0858 into main Feb 8, 2026
2 checks passed
@yamcodes yamcodes deleted the minimum-deletions-to-make-string-balanced branch February 8, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant