Minimum ASCII Delete Sum for Two Strings

Given two strings s1 and s2, return the lowest ASCII sum of deleted characters needed to make the two strings equal.

30:00

Minimum ASCII Delete Sum for Two Strings
medium
Free written solution
Topics
Companies

Given two strings s1 and s2, return the lowest ASCII sum of deleted characters needed to make the two strings equal.

Example 1:
Input: {"s1":"sea","s2":"eat"}
Output: 231
Constraints:
  • 1≤s1.length,s2.length≤10001 \leq \text{s1.length}, \text{s2.length} \leq 1000

  • s1 and s2 consist of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s1":"sea","s2":"eat"}

Build an (4) x (4) table: dp[i][j] = cheapest ASCII total of deletions that makes the first i letters of s1 equal the first j letters of s2.

Every letter has a price: its ASCII code. Deleting it costs that much.

s1

s

115

e

101

a

97

s2

e

101

a

97

t

116

∅
e
a
t
∅
s
e
a

↘ equal letters: keep both, cost 0

↓ pay for the s1 letter

→ pay for the s2 letter

Variables
VariableValue
m3
n3
DepthFunction Call
Stack empty
0/12