-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path161.cpp
More file actions
27 lines (27 loc) · 674 Bytes
/
161.cpp
File metadata and controls
27 lines (27 loc) · 674 Bytes
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
class Solution {
public:
bool isOneEditDistance(string s, string t) {
int m = s.size();
int n = t.size();
if (abs(m - n) == 1) {
// make s > t
if (m < n) {
swap(s, t);
swap(m, n);
}
int tIdx = 0;
for (int i = 0; i < m; i++) {
if (s[i] == t[tIdx]) tIdx++;
}
return tIdx == n;
}
else if (m == n) {
int count = 0;
for (int i = 0; i < m; i++) {
if (s[i] != t[i]) count++;
}
return count == 1;
}
else return false;
}
};