# One Edit Distance

## Edit Distance == 1

My first submission

```
return minDistance(s, t) == 1;
```

but, leetcode shows `time limit exceeded`

.

## Only 1 diff

### Same length

do one pass can know if there is only one different char

### 1 char longer

delete the redundant char will yield two same string

Note:

solved in `O(n)`

. Edit Distance == 1 is an `O(n * n)`

solution.

### Source code *Read on Github*

```
1 public class Solution {
2 public boolean isOneEditDistance(String s, String t) {
3
4 char[] S = s.toCharArray();
5 char[] T = t.toCharArray();
6
7 // make sure s is always to shorter one
8 if (S.length > T.length) return isOneEditDistance(t, s);
9
10 if (T.length - S.length > 1) return false;
11
12 int diff = 0;
13
14 int i = 0;
15 int j = 0;
16
17 for(/* void */; i < S.length; i++, j++){
18 if(S[i] != T[j]){
19
20 diff++;
21
22 if(diff > 1) return false;
23
24 // len(s) + 1 = len(t)
25 if(S.length != T.length && S[i] == T[j + 1]){
26 j++; // delete one from T
27 }
28 }
29 }
30
31 if (diff == 0){
32 return j + 1 == T.length;
33 }
34
35 return j == T.length;
36 }
37 }
```