Title: Edit Distance (Levenshtein Distance)

Find the edit distance between two given words s1 and s2. The distance is the minimum number of single-character edits required to change one word into the other. Edits include insertion, deletion, and substitution of characters.

Input:
s1
s2

Two words s1 and s2 are given in the first and second lines respectively. The words consist of lowercase characters.

Output:
Print the edit distance in a line.

Constraints:
- 1 ≤ length of s1 ≤ 1000
- 1 ≤ length of s2 ≤ 1000

Sample Input 1:
acac
acm

Sample Output 1:
2

Sample Input 2:
icpc
icpc

Sample Output 2:
0

# Buggy version:
s1 = input()
s2 = input()
x = [[0 for i in range(len(s2))]for j in range(len(s1))]

def g(i,j):
    if i == 0 or j == 0:
        if s1[i] == s2[j]:
            x[i][j] = abs(i-j)
        else:
            if i == 0:
                x[i][j] = x[i][j-1] + 1
            else:
                x[i][j] = x[i-1][j] + 1
    elif s1[i] == s2[j]:
        x[i][j] = x[i-1][j-1]
    else:
        x[i][j] = min(x[i][j-1] + 1,x[i-1][j] + 1)
        

for i in range(len(s1)):
    for j in range(len(s2)):
        g(i,j)

print(x[len(s1)-1][len(s2)-1])


# Fixed version:
s1 = input()
s2 = input()
x = [[0 for i in range(len(s2))]for j in range(len(s1))]

def g(i,j):
    if i == 0 or j == 0:
        if s1[i] == s2[j]:
            x[i][j] = abs(i-j)
        else:
            if i == 0:
                x[i][j] = x[i][j-1] + 1
            else:
                x[i][j] = x[i-1][j] + 1
    elif s1[i] == s2[j]:
        x[i][j] = min(x[i-1][j-1],x[i][j-1]+1,x[i-1][j]+1)
    else:
        x[i][j] = min(x[i-1][j-1]+1,x[i][j-1]+1,x[i-1][j]+1)
        

for i in range(len(s1)):
    for j in range(len(s2)):
        g(i,j)

print(x[len(s1)-1][len(s2)-1])

# Buggy version:
s1=input()
s2=input()
ls1=len(s1)
ls2=len(s2)
f=[[0 for j in range(ls2+1)] for i in range(ls1+1)]
for i in range(ls1+1):
    f[i][0]=i
for i in range(ls2+1):
    f[0][i]=i
for i in range(1,ls1+1):
    for j in range(1,ls2+1):
        if s1[i-1]!=s2[j-1]:
            f[i][j]=min(f[i-1][j]+1,f[i][j-1]+1,f[i-1][j-1]+1)
        else:
            f[i][j]=f[i-1][j-1]
print(f)



# Fixed version:
s1=input()
s2=input()
ls1=len(s1)
ls2=len(s2)
f=[[0 for j in range(ls2+1)] for i in range(ls1+1)]
for i in range(ls1+1):
    f[i][0]=i
for i in range(ls2+1):
    f[0][i]=i
for i in range(1,ls1+1):
    for j in range(1,ls2+1):
        if s1[i-1]!=s2[j-1]:
            f[i][j]=min(f[i-1][j]+1,f[i][j-1]+1,f[i-1][j-1]+1)
        else:
            f[i][j]=f[i-1][j-1]
print(f[i][j])

# Buggy version:
def search(X,Y):

 n=len(X)
 m=len(Y)
 p=[i for i in range(m+1)]
 for i in range(n):
  q=list(range(m+1))
  q[0] = i+1
  for j in range(m):
   if X[i]==Y[j]:
    cost=0
   else:
    cost=1
   q[j+1]=min(p[j+1]+1,q[j]+1,p[j]+cost)
  p,q=q,p
 
 return pre[m]


X=input()
Y=input()

print(search(X,Y))



# Fixed version:
def search(X,Y):

 n=len(X)
 m=len(Y)
 p=[i for i in range(m+1)]
 for i in range(n):
  q=list(range(m+1))
  q[0] = i+1
  for j in range(m):
   if X[i]==Y[j]:
    cost=0
   else:
    cost=1
   q[j+1]=min(p[j+1]+1,q[j]+1,p[j]+cost)
  p,q=q,p
 
 return p[m]


X=input()
Y=input()

print(search(X,Y))

# Buggy version:
a = list(input())
b = list(input())

if len(a) == 0 or len(b) == 0:
	print(max(len(a),len(b)))

else:
	dp = [[None for i in range(len(b)+1)] for j in range(len(a)+1)]
	for i in range(len(a)+1):
		dp[i][0] = i
	for i in range(len(b)+1):
		dp[0][i] = i

	for i in range(1,len(a)+1):
		for j in range(len(b)+1):
			aaa = dp[i-1][j-1] if a[i-1] == b[j-1] else dp[i-1][j-1]+1
			dp[i][j] = min(aaa,dp[i-1][j] + 1,dp[i][j-1]+1)

print(dp[-1][-1])


# Fixed version:
a = list(input())
b = list(input())

if len(a) == 0 or len(b) == 0:
	print(max(len(a),len(b)))

else:
	dp = [[None for i in range(len(b)+1)] for j in range(len(a)+1)]
	for i in range(len(a)+1):
		dp[i][0] = i
	for i in range(len(b)+1):
		dp[0][i] = i

	for i in range(1,len(a)+1):
		for j in range(1,len(b)+1):
			aaa = dp[i-1][j-1] if a[i-1] == b[j-1] else dp[i-1][j-1]+1
			dp[i][j] = min(aaa,dp[i-1][j] + 1,dp[i][j-1]+1)

print(dp[-1][-1])

# Buggy version:
s_1 = input()
s_2 = input()

memo = [[0 for i in range(len(s_2)+1)] for j in range(len(s_1) + 1)]
flag = [[False for i in range(len(s_2)+1)] for j in range(len(s_1) + 1)]

for i in range(1, len(s_1)+1):
    for j in range(1, len(s_2)+1):
        if not flag[i][j]:
            flag[i][j] = True
            if s_1[i-1] == s_2[j-1]:
                memo[i][j] = min(memo[i-1][j-1], memo[i][j-1]+1)
                memo[i][j] = min(memo[i][j], memo[i-1][j]+1)
            else:
                memo[i][j] = min(memo[i-1][j-1]+1, memo[i][j-1]+1)
                memo[i][j] = min(memo[i][j], memo[i-1][j]+1)

print(memo[len(s_1)][len(s_2)])

# ababa
# babab



# Fixed version:
s_1 = input()
s_2 = input()

memo = [[0 for i in range(len(s_2)+1)] for j in range(len(s_1) + 1)]
flag = [[False for i in range(len(s_2)+1)] for j in range(len(s_1) + 1)]

for i in range(1, len(s_1)+1):
    memo[i][0] = i
for j in range(1, len(s_2)+1):
    memo[0][j] = j

for i in range(1, len(s_1)+1):
    for j in range(1, len(s_2)+1):
        if not flag[i][j]:
            flag[i][j] = True
            if s_1[i-1] == s_2[j-1]:
                memo[i][j] = min(memo[i-1][j-1], memo[i][j-1]+1)
                memo[i][j] = min(memo[i][j], memo[i-1][j]+1)
            else:
                memo[i][j] = min(memo[i-1][j-1]+1, memo[i][j-1]+1)
                memo[i][j] = min(memo[i][j], memo[i-1][j]+1)

print(memo[len(s_1)][len(s_2)])

# Buggy version:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def leven(s1, s2):
    n1 = len(s1)
    n2 = len(s2)
    dp = [[0]*(n1+1) for _ in range(n2+1)]

    for i in range(1, n1+1):
        dp[0][i] = i
    for i in range(1, n2+1):
        dp[i][0] = i

    for j in range(1, n2+1):
        for k in range(1, n1+1):
            m = min(dp[j-1][k]+1, dp[j][k-1])
            if s1[j-1] == s2[k-1]:  # 最後の文字削除
                m = min(m, dp[j-1][k-1])
            else:
                m = min(m, dp[j-1][k-1]+1)
            dp[j][k] = m
    print(dp[-1][-1])


s1 = input()[:-1]
s2 = input()[:-1]
leven(s1, s2)



# Fixed version:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def leven(s1, s2):
    n1 = len(s1)
    n2 = len(s2)
    dp = [[0]*(n1+1) for _ in range(n2+1)]

    for i in range(1, n1+1):
        dp[0][i] = i
    for i in range(1, n2+1):
        dp[i][0] = i

    for j in range(1, n2+1):
        for k in range(1, n1+1):
            m = min(dp[j-1][k], dp[j][k-1])+1
            if s1[k-1] == s2[j-1]:  # 最後の文字削除
                m = min(m, dp[j-1][k-1])
            else:
                m = min(m, dp[j-1][k-1]+1)
            dp[j][k] = m
    print(dp[-1][-1])


s1 = input()[:-1]
s2 = input()[:-1]
leven(s1, s2)