Min Cost to Make Two Strings Equal

Day 10 Problem Statement: The goal is to make two strings exactly the same by deleting characters, where deleting a character costs its ASCII value. We want to do this with the minimum total cost. Approach: Start comparing both strings from the beginning. At any position, you have two pointers: i pointing to s1 j pointing to s2 If the characters at these positions are the same, keep them and move both pointers forward (no cost). If the characters are different, you have two choices: Delete the character from s1 (pay its ASCII value and move i) Delete the character from s2 (pay its ASCII value and move j) Choose the option that costs less overall. If one string finishes, you must delete all remaining characters from the other string. Why Dynamic Programming is needed The same (i, j) positions appear again and again during recursion. To avoid recomputing, store the result for each (i, j) in a DP table. dp[i][j] represents: Minimum delete cost to make s1[i…] and s2[j…] equal. #HappyCoding #LeetCodeDaily #Java #DSA #ProblemSolving #Consistency

To view or add a comment, sign in

Explore content categories