Problem:
Edit Distance (Levenshtein Distance)

Find the edit distance between given two words s1 and s2.

The disntace is the minimum number of single-character edits required to change one word into the other. The edits including the following operations:

insertion: Insert a character at a particular position.

deletion: Delete a character at a particular position.

substitution: Change the character at a particular position to a different character

Input

s1
s2

Two words s1 and s2 are given in the first line and the second line respectively. The words will consist of lower case 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

Solution in Scala (judge verdict: Accepted)

import java.util.Scanner

object Main extends App { 
  val sc = new Scanner(System.in)
  val S1, S2 = sc.nextLine()

  class DP(s0:String, s1:String) {
    val a0 = ("*"+s0).toCharArray
    val a1 = ("*"+s1).toCharArray
    val hight = a0.length
    val width = a1.length
    val memo = Array.ofDim[Int](hight, width)
    for (i <- 0 until hight) memo(i)(0) = i
    for (j <- 0 until width) memo(0)(j) = j

    def solver():Int = {
      for (i <- 1 until hight; j <- 1 until width) {
        val ci = memo(i-1)(j)+1
        val cd = memo(i)(j-1)+1
        val cr = memo(i-1)(j-1) + (if (a0(i) == a1(j)) 0 else 1)
        memo(i)(j) = List(ci,cd,cr).min
      }
      memo(hight-1)(width-1)      
    }
  }

  val dp = new DP(S1, S2)

  println(dp.solver())
}