Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,24 @@
/**
* Solution for the <b>Minimum Deletions to Make String Balanced</b> problem.
*
* <p>Uses a split-point counting approach to compute minimum deletions in constant space.
* <p>Optimizes {@code SolutionDp} by collapsing the DP table into a single variable.
*
* <ul>
* <li><b>Time Complexity:</b> <code>O(n)</code>
* <li><b>Space Complexity:</b> <code>O(1)</code>
* </ul>
*/
class Solution {
/**
* Returns the minimum number of deletions to make {@code s} balanced.
*
* @param s the string of {@code 'a'} and {@code 'b'} characters
* @return the minimum number of deletions needed
*/
int minimumDeletions(String s) {
int bCount = 0;
int aAfter = 0;
int bBefore = 0;
for (int i = 0; i < s.length(); i++) {
int bCount = s.charAt(0) == 'b' ? 1 : 0;
int minDel = 0;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == 'b') {
bCount++;
} else {
aAfter++;
}
if (aAfter + bBefore > bCount) {
bBefore = bCount;
aAfter = 0;
minDel = Math.min(minDel + 1, bCount);
}
}
return Math.min(bCount, aAfter + bBefore);
return minDel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Bottom-up DP solution for the <b>Minimum Deletions to Make String Balanced</b> problem.
*
* <p>Flips the memoized recursion into an iterative DP table.
* <p>Iterative DP table tracking minimum deletions at each position.
*
* <ul>
* <li><b>Time Complexity:</b> <code>O(n)</code>
Expand All @@ -17,9 +17,18 @@ class SolutionDp {
* @param s the string of {@code 'a'} and {@code 'b'} characters
* @return the minimum number of deletions needed
*/
@SuppressWarnings("unused")
int minimumDeletions(String s) {
// TODO: Implement bottom-up DP solution
throw new UnsupportedOperationException("Not yet implemented");
int n = s.length();
int[] dp = new int[n];
int bCount = s.charAt(0) == 'b' ? 1 : 0;
for (int i = 1; i < n; i++) {
if (s.charAt(i) == 'b') {
dp[i] = dp[i - 1];
bCount++;
} else {
dp[i] = Math.min(dp[i - 1] + 1, bCount);
}
}
return dp[n - 1];
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package codes.yam.leetcode.minimumdeletionstomakestringbalanced;

/**
* Solution for the <b>Minimum Deletions to Make String Balanced</b> problem.
*
* <p>Uses a split-point counting approach to compute minimum deletions in constant space.
*
* <ul>
* <li><b>Time Complexity:</b> <code>O(n)</code>
* <li><b>Space Complexity:</b> <code>O(1)</code>
* </ul>
*/
class SolutionSplitPoint {
/**
* Returns the minimum number of deletions to make {@code s} balanced.
*
* @param s the string of {@code 'a'} and {@code 'b'} characters
* @return the minimum number of deletions needed
*/
int minimumDeletions(String s) {
int bCount = 0;
int aAfter = 0;
int bBefore = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'b') {
bCount++;
} else {
aAfter++;
}
if (aAfter + bBefore > bCount) {
bBefore = bCount;
aAfter = 0;
}
}
return Math.min(bCount, aAfter + bBefore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
* </code> space
* </ol>
*
* <p><b>Solution progression (split-point, optimized):</b>
*
* <ol start="3">
* <li>{@code SolutionSplitPoint} — Split-point counting, <code>O(n)</code> time, <code>O(1)
* </code> space
* </ol>
*
* <p><b>Solution progression (DP approach):</b>
*
* <ol>
* <li>{@code SolutionRecursive} — Brute force recursion, <code>O(2^n)</code> time, <code>O(n)
* </code> space
* <li>{@code SolutionMemoized} — Memoized recursion, <code>O(n²)</code> time, <code>O(n²)</code>
* space
* <li>{@code SolutionDp} — Bottom-up DP, <code>O(n)</code> time, <code>O(n)</code> space
* <li>{@code Solution} — Space-optimized DP, <code>O(n)</code> time, <code>O(1)</code> space
* </ol>
*
* <p><b>TODO:</b> Current {@code Solution} uses a split-point approach. Once the DP progression is
* implemented, replace it with the cleaner DP formulation (rename current to {@code
* SolutionSplitPoint}).
*
* @see <a href="https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/">Problem
* Link</a>
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

@Disabled("Not yet implemented")
class SolutionMemoizedTest {
class SolutionSplitPointTest {
@ParameterizedTest
@MethodSource("codes.yam.leetcode.minimumdeletionstomakestringbalanced.TestCases#cases")
void minimumDeletions(String s, int expected) {
assertEquals(expected, new SolutionMemoized().minimumDeletions(s));
assertEquals(expected, new SolutionSplitPoint().minimumDeletions(s));
}
}