-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdit-Distance.cpp
More file actions
19 lines (18 loc) · 904 Bytes
/
Copy pathEdit-Distance.cpp
File metadata and controls
19 lines (18 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Company tags: Accenture, Adobe, Amazon, Apple, Arcesium, Bloomberg, DE Shaw, EPAM Systems, Flipkart, Google Infosys, Instacart, LinkedIn, Meta, Microsoft, Rubrik, Salesforce, Snap, TCS, TikTok, Uber, Yahoo, Yandex, Zoho
class Solution {
public:
// Time Complexity : O(n * m)
// Space Complexity : O(n * m)
int t[501][501];
int solve(string& word1, string& word2, int i, int j){
if (i == word1.size()) return word2.size() - j;
if (j == word2.size()) return word1.size() - i;
if(t[i][j] != -1) return t[i][j];
if(word1[i] == word2[j]) return t[i][j] = solve(word1, word2, i+1, j+1);
else return t[i][j] = 1 + min({solve(word1, word2, i+1, j), solve(word1, word2, i, j+1), solve(word1, word2, i+1, j+1)});
}
int minDistance(string word1, string word2) {
memset(t, -1, sizeof(t));
return solve(word1, word2, 0, 0);
}
};