161. One Edit Distance

161. One Edit Distance

Description

Given two strings s and t, return true if they are both one edit distance apart, otherwise return false.

A string s is said to be one distance apart from a string t if you can:

  • Insert exactly one character into s to get t.
  • Delete exactly one character from s to get t.
  • Replace exactly one character of s with a different character to get t.

Example 1:

1
2
3
Input: s = "ab", t = "acb"
Output: true
Explanation: We can insert 'c' into sto gett.

Example 2:

1
2
3
Input: s = "", t = ""
Output: false
Explanation: We cannot get t from s by only one step.

Constraints:

  • 0 <= s.length, t.length <= 10^4
  • s and t consist of lowercase letters, uppercase letters, and digits.

Hints/Notes

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
bool isOneEditDistance(string s, string t) {
int idx1 = 0, idx2 = 0;
int n1 = s.size(), n2 = t.size();
if (abs(n1 - n2) > 1) {
return false;
}
if (n1 > n2) return isOneEditDistance(t, s);
// so t is always the one shorter
for (int i = 0; i < n1; i++) {
if (s[i] != t[i]) {
if (n1 != n2) {
return s.substr(i) == t.substr(i + 1);
} else {
return s.substr(i + 1) == t.substr(i + 1);
}
}
}
// if we reach here, it means we didn't find unmatched character
// if n1 == n2, then they are the same
return n1 != n2;
}
};