forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
70 lines (50 loc) · 1.64 KB
/
main.cpp
File metadata and controls
70 lines (50 loc) · 1.64 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/// Source : https://leetcode.com/problems/one-edit-distance/
/// Author : liuyubobobo
/// Time : 2019-02-14
#include <iostream>
#include <cassert>
using namespace std;
/// Three One Pass Algorithms
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
bool isOneEditDistance(string s, string t) {
if(s.size() == 0) return t.size() == 1;
if(t.size() == 0) return s.size() == 1;
if(s.size() + 1 == t.size())
return can_add_char(s, t);
else if(s.size() == t.size() + 1)
return can_delete_char(s, t);
else if(s.size() == t.size())
return can_replace_char(s, t);
return false;
}
private:
bool can_replace_char(const string& s, const string& t){
assert(s.size() == t.size());
int diff = 0;
for(int i = 0; i < s.size(); i ++)
diff += s[i] != t[i];
return diff == 1;
}
bool can_delete_char(const string& s, const string& t){
return can_add_char(t, s);
}
bool can_add_char(const string&s, const string& t){
int si = 0, ti = 0;
while(si < s.size() && ti < t.size() && s[si] == t[ti]) si ++, ti ++;
si --;
int sj = s.size() - 1, tj = t.size() - 1;
while(sj > si && tj > ti && s[sj] == t[tj]) sj --, tj --;
// cout << "si=" << si << " sj=" << sj << " ti=" << ti << " tj=" << tj << endl;
return si == sj;
}
};
int main() {
string s1 = "ab", t1 = "acb";
cout << Solution().isOneEditDistance(s1, t1) << endl;
string s2 = "cab", t2 = "ad";
cout << Solution().isOneEditDistance(s2, t2) << endl;
return 0;
}