C Example: Levenshtein Distance: DP matrix and edit costs
Computes edit distance with a 2D DP: dp[i][j] takes the minimum of delete, insert and substitute, with zero substitution cost for equal characters. Prints the full 8×8 matrix and distance 3 for kitten→sitting, then tests flaw→lawn and identical strings.
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
_ s i t t i n g
_ | 0 1 2 3 4 5 6 7
k | 1 1 2 3 4 5 6 7
i | 2 2 1 2 3 4 5 6
t | 3 3 2 1 2 3 4 5
t | 4 4 3 2 1 2 3 4
e | 5 5 4 3 2 2 3 4
n | 6 6 5 4 3 3 2 3
kitten -> sitting distance=3
_ l a w n
_ | 0 1 2 3 4
f | 1 1 2 3 4
l | 2 1 2 3 4
a | 3 2 1 2 3
w | 4 3 2 1 2
flaw -> lawn distance=2
_ C
_ | 0 1
C | 1 0
C -> C distance=0
🧩 Keep Learning