```
class Solution {
public:
bool isOneEditDistance(string s, string t) {
int m = (int)s.length(), n = (int)t.length();
if (abs(m - n) > 1) return false;
int i = 0, j = 0, dist = 0;
while (dist < 2 && i < m && j < n) {
if (s[i] != t[j]) {
// make sure there are equal number characters left when distance is incremented.
if (m - i < n - j) ++j;
else if (m - i > n - j) ++i;
else ++i, ++j;
++dist;
} else
++i, ++j;
}
dist += (m - i) + (n - j); // dist is not necessarily the actual distance if the distance is already greater than 1.
return dist == 1;
}
};
```