#K94797. Edit Distance Problem
Edit Distance Problem
Edit Distance Problem
Given two strings s1 and s2, your task is to compute the minimum number of edit operations needed to transform s1 into s2. In one operation, you can:
- Insert a character
- Delete a character
- Replace a character
This problem is equivalent to finding the Levenshtein distance between the two strings. The recurrence relation for the dynamic programming solution is given by:
\(dp(i,j) = \begin{cases} \; i, & \text{if } j=0 \\ \; j, & \text{if } i=0 \\ \; dp(i-1,j-1), & \text{if } s_1[i-1] = s_2[j-1] \\ \; 1 + \min\{dp(i-1,j),\, dp(i,j-1),\, dp(i-1,j-1)\}, & \text{otherwise} \end{cases}\)
where \(dp(i,j)\) is the edit distance between the first \(i\) characters of s1 and the first \(j\) characters of s2.
inputFormat
The input consists of two lines:
- The first line contains the string s1.
- The second line contains the string s2.
Note: The strings can be empty.
outputFormat
Output a single integer, which is the minimum number of edit operations required to convert s1 into s2.
## samplehorse
ros
3