-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneEditDistance.cc
More file actions
37 lines (34 loc) · 1.27 KB
/
OneEditDistance.cc
File metadata and controls
37 lines (34 loc) · 1.27 KB
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
// https://oj.leetcode.com/problems/one-edit-distance/
// Several cases:
// "", "" --> false, because they are exactly same
// "a", "a" --> false, because they are exactly same
// "a", "" --> true
// "a", "ac" --> true
// "a", "A" --> true
class Solution {
public:
bool isOneEditDistance(string s, string t) {
int diff = s.size() - t.size();
if (diff > 1 || diff < -1) return false;
bool foundOneDiff = false;
int i = 0;
int j = 0;
while (i < s.size() && j < t.size()) {
if (s[i] == t[j]) {
i++; j++;
} else {
if (foundOneDiff) return false;
if (s.size() == t.size()) {
i++; j++;
} else if (s.size() > t.size()) {
i++;
} else {
j++;
}
foundOneDiff = true;
}
}
if (i == s.size() && j == t.size()) return foundOneDiff; // if we just arrive at the ends of both string, return true if there is only one diff found
if (i == s.size() || j == t.size()) return !foundOneDiff; // if we arrive at the end of just one string, we have to consider the diff due to length difference as another diff.
}
};