#include <string>
#include <iostream>
using namespace std;
/*
Given two strings S and T, determine if they are both one edit distance apart.
*/
bool isOneEditDistance(string s, string t) {
int sSize = s.size();
int tSize = t.size();
if(abs(sSize - tSize) > 1) return false;
int count = 0;
if(sSize == tSize) {
int i = 0, j = 0;
while(i < sSize && j < tSize) {
if(s[i] != t[i]) {
count++;
if(count > 1) return false;
}
i++; j++;
}
return true;
} else {
string tmp = sSize > tSize ? s : t;
string tmp_1 = sSize > tSize ? t : s;
int i = 0, j = 0;
while(i < sSize && j < tSize) {
if((tmp[i] != tmp_1[j]) && (count != 0)) {
return false;
} else if(tmp[i] != tmp_1[j]) {
count++;
i++;
} else {
i++;
j++;
}
}
return true;
}
}
int main(void) {
string a = "abc";
string b = "ab";
cout << isOneEditDistance(a, b) << endl;
}
~
LeetCode 161. One Edit Distance
最新推荐文章于 2022-06-10 23:58:47 发布