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
@@ -0,0 +1,55 @@
package com.github.mstepan.leetcode.medium;

import java.util.Objects;

/**
* 712. Minimum ASCII Delete Sum for Two Strings
*
* <p>https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/
*/
public class MinimumAsciiDeleteSumForTwoStrings {

/**
* Solved using bottom-up dynamic programming approach.
*
* <p>time: O(N*K)
*
* <p>space: O(N*K) but can be reduced to O(min(N,K))
*
* <p>N = s1.length(), K = s2.length()
*/
public static int minimumDeleteSum(String s1, String s2) {
Objects.requireNonNull(s1);
Objects.requireNonNull(s2);
final int rows = s1.length() + 1;
final int cols = s2.length() + 1;
int[][] opt = new int[rows][cols];

for (int row = 1; row < rows; ++row) {
opt[row][0] = opt[row - 1][0] + s1.charAt(row - 1);
}

for (int col = 1; col < cols; ++col) {
opt[0][col] = opt[0][col - 1] + s2.charAt(col - 1);
}

for (int row = 1; row < rows; ++row) {
for (int col = 1; col < cols; ++col) {

char c1 = s1.charAt(row - 1);
char c2 = s2.charAt(col - 1);

if (c1 == c2) {
opt[row][col] = opt[row - 1][col - 1];
} else {
opt[row][col] =
Math.min(
opt[row - 1][col] + s1.charAt(row - 1),
opt[row][col - 1] + s2.charAt(col - 1));
}
}
}

return opt[rows - 1][cols - 1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.mstepan.leetcode.medium;

import static com.github.mstepan.leetcode.medium.MinimumAsciiDeleteSumForTwoStrings.minimumDeleteSum;

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

import org.junit.jupiter.api.Test;

public class MinimumAsciiDeleteSumForTwoStringsTest {

@Test
void case1() {
assertEquals(231, minimumDeleteSum("sea", "eat"));
}

@Test
void case2() {
assertEquals(403, minimumDeleteSum("delete", "leet"));
}
}
Loading