Minimum ASCII Sum of Deleted Characters to Equal Strings

Day 92/100 Problem Statement : Given two strings s1 and s2, return the lowest ASCII sum of deleted characters to make two strings equal. Input: s1 = "sea", s2 = "eat" Output: 231 Solution : https://lnkd.in/gm8-CDmm public int minimumDeleteSum(String s1, String s2) { int m = s1.length(), n = s2.length(); int[][] dp = new int[m + 1][n + 1]; for (int i = m - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { if (s1.charAt(i) == s2.charAt(j)) { dp[i][j] = s1.charAt(i) + dp[i + 1][j + 1]; } else { dp[i][j] = Math.max(dp[i + 1][j], dp[i][j + 1]); } } } int total = 0; for (char c : s1.toCharArray()) total += c; for (char c : s2.toCharArray()) total += c; return total - 2 * dp[0][0]; } #100DaysDSA #100DaysOfCode #Java #Leetcode #Neetcode #Neetcode250 #TUF

To view or add a comment, sign in

Explore content categories