```
bool isOneEditDistance(string s, string t) {
int m = s.length(), n = t.length(), cnt = 0; //cnt counts the number of distances
for(int i = 0, j = 0; (i<m || j<n) && cnt<=1; i++,j++){
if(s[i]!=t[j]){
cnt++;
if(m<n)i--; //if m<n, then t[j] goes to t[j+1] while s[i] stays
if(m>n)j--; //if m>n, then s[i] goes to s[i+1] while t[j] stays
}
}
return cnt==1;
}
```