Minimize ASCII Delete Sum for Two Strings with Dynamic Programming

📅 Day 64 of #100DaysOfLeetCode 🧮 Problem: 712. Minimum ASCII Delete Sum for Two Strings Difficulty: Medium 🧠 Key Idea The problem is a Dynamic Programming on strings variant, similar to Edit Distance. Instead of minimizing operations, we minimize the sum of ASCII values of deleted characters to make both strings equal. We compare prefixes of both strings and decide whether to: keep matching characters, or delete a character from either string with minimum cost. ✅ Approach (Bottom-Up DP / Tabulation) State Definition: dp[i][j] → Minimum ASCII delete sum to make s1[0…i-1] and s2[0…j-1] equal. 🔹 Step 1: Base Cases If s2 is empty → delete all characters of s1 If s1 is empty → delete all characters of s2 🔹 Step 2: DP Transition If characters match → no deletion needed Else → choose the cheaper delete: delete from s1 delete from s2 🔹 Final Answer Stored in dp[n][m] ⏱ Complexity Time: O(n × m) Space: O(n × m) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday

  • text

To view or add a comment, sign in

Explore content categories