Skip to content

Commit cb0b3dc

Browse files
committed
May Day 31. Yaay Finally!!!
1 parent 476a686 commit cb0b3dc

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int minDistance(string word1, string word2) {
4+
int len1 = word1.size(), len2 = word2.size();
5+
vector<vector<int>> dp(2, vector<int>(len1 + 1, 0));
6+
for (int i = 0; i <= len1; i++) {
7+
dp[0][i] = i;
8+
}
9+
for (int i = 1; i <= len2; i++) {
10+
for (int j = 0; j <= len1; j++) {
11+
if (j == 0) {
12+
dp[i % 2][j] = i;
13+
}
14+
else if (word1[j - 1] == word2[i - 1]) {
15+
dp[i % 2][j] = dp[(i - 1) % 2][j - 1];
16+
}
17+
else {
18+
dp[i % 2][j] = 1 + min({dp[(i-1) % 2][j], dp[i % 2][j - 1], dp[(i - 1) % 2][j - 1]});
19+
}
20+
}
21+
}
22+
return dp[len2 % 2][len1];
23+
}
24+
};

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,4 @@ Since I am completing this in May, I could not access the 7th question of each w
139139
| --- | ------------------------------------------------------------------------------ |
140140
| 29 | [Course Schedule](MayChallenge/Week5/29:CourseSchedule.cpp) |
141141
| 30 | [K Closest Points to Origin](MayChallenge/Week5/30:KClosestPointstoOrigin.cpp) |
142-
| 31 | |
142+
| 31 | [Edit Distance](MayChallenge/Week5/31:EditDistance.cpp) |

0 commit comments

Comments
 (0)