-
Notifications
You must be signed in to change notification settings - Fork 28
/
One_Edit_Distance.cpp
38 lines (38 loc) · 1.02 KB
/
One_Edit_Distance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
bool isOneEditDistanceUtil(string const& s, string const& t) {
int sLen = s.length();
int tLen = t.length();
if(tLen > sLen) {
return isOneEditDistanceUtil(t, s);
}
bool flag = false;
for(int i = 0, k = 0; i < tLen; ++i, ++k) {
if(t[i] != s[k]) {
if(flag) {
return false;
}
k++;
flag = true;
}
if(s[k] != t[i]) {
return false;
}
}
return true;
}
public:
bool isOneEditDistance(string s, string t) {
int sLen = s.length();
int tLen = t.length();
if(sLen == tLen) {
int different = 0;
for(int i = 0; i < sLen; ++i) {
different += (s[i] != t[i]);
}
return (different == 1);
} else if(abs(sLen - tLen) == 1) {
return isOneEditDistanceUtil(s, t);
}
return false;
}
};