# One Edit Distance

Given two strings**s** and**t**, determine if they are both one edit distance apart.

**Note:**

There are 3 possiblities to satisfy one edit distance apart:

1. Insert a character into ***s*** to get ***t***
2. Delete a character from ***s** \_to get* **t**\_
3. Replace a character of ***s*** to get ***t***

## Example

**Example 1:**

```
Input:
s = "ab", 
t = "acb"

Output: true

Explanation:
 We can insert 'c' into s
 to get t.
```

**Example 2:**

```
Input:
s = "cab", 
t = "ad"

Output:
 false

Explanation:
 We cannot get t from s by only one step.
```

**Example 3:**

```
Input:
s = "1203", 
t = "1213"

Output:
 true

Explanation:
 We can replace '0' with '1' to get t.
```

## Note

当当前位置不一样的时候，根据三种情况分别比较substring

edge case 长度差一，最后多一位，最后判断一下

```
Math.abs(s.length() - t.length()) == 1;
```

## Code

```java
public boolean isOneEditDistance(String s, String t) {
    for (int i = 0; i < Math.min(s.length(), t.length()); i++) {
        if (s.charAt(i) != t.charAt(i)) {
            if (s.length() == t.length()) {
                return s.substring(i + 1).equals(t.substring(i + 1));    
            } else if (s.length() > t.length()) {
                return s.substring(i + 1).equals(t.substring(i));
            } else {
                return s.substring(i).equals(t.substring(i + 1));
            }
        }
    }
    return Math.abs(s.length() - t.length()) == 1;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://luj.gitbook.io/code/string/1-pattern/one-edit-distance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
