def diff n, m = gets.chomp.split(" ").map(&:to_i) s = gets.chomp t = gets.chomp inf = 100000007 dp = Array.new(n+2){Array.new(m+2, inf)} dp[0][0] = 0 0.upto n do |i| 0.upto m do |j| dp[i+1][j+1] = s[i] == t[j] ? [dp[i+1][j+1], dp[i][j]].min : [dp[i+1][i+1], dp[i][j] + 1 ].min dp[i+1][j] = [dp[i+1][j], dp[i][j] + 1].min dp[i][j+1] = [dp[i][j+1], dp[i][j] + 1].min end end dp[n][m] end puts diff