Topic
Given words word1 and word2, find the minimum number of steps required to convert Word1 to Word2. (Each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
A) Insert a character
b) Delete a character
c) Replace a character
Ideas
Specific reference: [Classic face question] string editing distance
The idea of a timeout
Code
/* --------------------------------------------* Date: 2014-03-01 * SJF0115 * Title: 72.Edit Distance * Website: https://oj.leetcode.com/problems/edit-distance/* Result: AC * Source: Leetcode * Blog:------------------------- -----------------------*/ #include <iostream> #include <vector> using namespace STD;classSolution { Public:intMindistance (stringWord1,stringWORD2) {intm = Word1.size ();intn = word2.size ();///EDIT[I][J] to word1[0..i-1] and word2[0..j-1] minimum number of edits intedit[m+1][n+1];//Initialize for(inti =0; I <= m;++i) {edit[i][0] = i; }//for for(inti =0; I <= n;++i) {edit[0][i] = i; }//for for(inti =1; I <= m;++i) { for(intj =1; J <= N;++j) {//The current character is the same if(word1[i-1] = = word2[j-1]) {Edit[i][j] = edit[i-1][j-1]; }//if Else{Edit[i][j] =1+ min (edit[i-1][j-1],min (edit[i-1][j],edit[i][j-1])); }//else}//for}//for returnEdit[m][n]; } };intMain () {solution solution;stringSTR1 ("AB");stringSTR2 ("BC");cout<<solution.mindistance (STR1,STR2) <<endl;return 0; }
Run time
[Leetcode]72.edit Distance