def diff n, m = gets.chomp.split(" ").map(&:to_i) s = gets.chomp t = gets.chomp inf = 100000007 dp = Array.new(n+1){Array.new(m+1, inf)} dp[0][0] = 0 n.times do |i| m.times do |j| dp[i+1][j+1] = s[i] == t[j] ? [dp[i+1][j+1], dp[i][j]].min : [dp[i+1][j+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