Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 72-Edit-Distance.cpp
C++ solution for 72-Edit-Distance
  • Loading branch information
Pegasus02K authored Apr 15, 2022
commit 6a41f9a5fbd4fd8e852ce84cb4f2c6f1ed35e464
23 changes: 23 additions & 0 deletions cpp/72-Edit-Distance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int minDistance(string word1, string word2) {
// cache for edit distance between substrings or word1 and word2 containing 0 letter to full word
vector<vector<int>> cache(word1.size() + 1, vector<int>(word2.size() + 1));

// if one word is empty, edit distance is adding all letters of the other word
for (int i = 0; i <= word1.size(); ++i)
cache[i][0] = i;
for (int j = 0; j <= word2.size(); ++j)
cache[0][j] = j;

for (int i = 1; i <= word1.size(); ++i) {
for (int j = 1; j <= word2.size(); ++j) {
if (word1[i - 1] == word2[j - 1]) // if current letters are the same, edit distance is same as before
cache[i][j] = cache[i - 1][j - 1];
else // else the distance is min of (replace, add, delete)
cache[i][j] = min(cache[i - 1][j - 1], min(cache[i - 1][j], cache[i][j - 1])) + 1;
}
}
return cache[word1.size()][word2.size()];
}
};