# 161. One Edit Distance
Given two strings s and t, determine if they are both one edit distance apart.
Note:
There are 3 possiblities to satisify one edit distance apart:
Insert a character into s to get t
Delete a character from s to get t
Replace a character of s to get t
<pre>
Example 1:
Input: s = "ab", t = "acb"
Output: true
Explanation: We can insert 'c' into s to get t.
</pre>
<pre>
Example 2:
Input: s = "cab", t = "ad"
Output: false
Explanation: We cannot get t from s by only one step.
</pre>
<pre>
Example 3:
Input: s = "1203", t = "1213"
Output: true
Explanation: We can replace '0' with '1' to get t.
</pre>
<details>
<summary>Source code</summary>
**Method 1**
* Naive
```c++=
class Solution {
public:
bool isOneEditDistance(string s, string t) {
if (s.size() < t.size()) return isOneEditDistance(t, s);
if (s.size() - t.size() >= 2) return false;
bool used = false;
if (s.size() == t.size()) {
for (int i = 0; i < s.size(); i++) {
if (s[i] == t[i]) {
continue;
} else {
used = !used;
if (!used) {
return false;
}
}
}
}
else {
for (int i = 0, j = 0; i < s.size(); i++, j++) {
if (s[i] == t[j]) {
continue;
} else {
used = !used;
if (!used) {
return false;
}
j--;
}
}
}
return (used == true);
}
};
```
**Method 2**
* Use substr
```c++=
class Solution {
public:
bool isOneEditDistance(string s, string t) {
if (s.size() < t.size()) return isOneEditDistance(t, s);
if (s.size() - t.size() >= 2) return false;
for (int i = 0; i < s.size(); i++) {
if (i >= t.size()) return true;
if (s[i] == t[i]) continue;
return (s.size() > t.size()) ? (s.substr(i + 1) == t.substr(i))
: (s.substr(i + 1) == t.substr(i + 1));
}
return false;
}
};
```
</details>