# 2019-08-05 EP161 One Edit Distance

image.png

``````class Solution {
public:
bool isOneEditDistance(string s, string t) {
vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1, 0));

for (int i = 0; i <= s.size(); i++) {
dp[i][0] = i;
}

for (int j = 0; j < t.size(); j++) {
dp[0][j] = j;
}

for (int i = 1; i <= s.size(); i++) {
for (int j = 1; j <= t.size(); j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
}
}
}

return dp[s.size()][t.size()] == 1;
}
};
``````

• 如果两个string 长度相等，那么有且只有其中的个char值不一样
• 如果两个string 长度不相等，那么长度相差应该有且等于1，且不同的地方之后应该和原来的string相等
``````class Solution {
public:
bool isOneEditDistance(string s, string t) {
int m = s.size(), n = t.size();
if (m > n) {
return isOneEditDistance(t, s);
}
for (int i = 0; i < m; i++) {
if (s[i] != t[i]) {
if (m == n) {
return s.substr(i + 1) == t.substr(i + 1);
}
return s.substr(i) == t.substr(i + 1);
}
}
return m + 1 == n;
}
};
``````