---
# System prepended metadata

title: 0161. One Edit Distance
tags: [Leetcode, FaceBook, Medium]

---

# 0161. One Edit Distance
###### tags: `Leetcode` `FaceBook` `Medium`
Link: https://leetcode.com/problems/one-edit-distance/
## 思路
思路不难想
但是值得记住的部分是```if(s.length()<t.length()) return isOneEditDistance(t,s);```来保证进入函数的两个字串，字串长度第一个大于等于第二个，以减少分类讨论的情况
## Code
```java=
class Solution {
    public boolean isOneEditDistance(String s, String t) {
        if(s.length()<t.length()) return isOneEditDistance(t,s);
        if(Math.abs(s.length()-t.length())>1) return false;
        for(int i = 0;i < 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{
                    return (s.substring(i+1).equals(t.substring(i)));                    
                }
            }
        }
        return s.length() == t.length()+1;
    }
}
```