Implement minimum-deletions-to-make-string-balanced#8
Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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. WalkthroughThis 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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; }
Implement minimum-deletions-to-make-string-balanced
Summary by CodeRabbit
Release Notes
New Features
Tests