LeetCode 161. One Edit Distance (Java版; Medium)

welcome to my blog

LeetCode 161. One Edit Distance (Java版; Medium)

题目描述
Given two strings s and t, determine if they are both one edit distance apart.

Note: 

There are 3 possiblities to satisify one edit distance apart:

Insert a character into s to get t
Delete a character from s to get t
Replace a character of s to get t
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.
第一次做; 核心: 1) 分成两种情况讨论: 两字符串长度相等, 两字符串长度不等; 2) 两字符串长度不相等时, 依次比较对应位置的字符是否相等, 如果不相等则删除更长的字符串中该位置的字符, 剩余的部分应该和更短的字符串的剩余部分相等
class Solution {
    public boolean isOneEditDistance(String s, String t) {
        int m = s.length(), n = t.length();
        if(Math.abs(m-n)>1){
            return false;
        }
        
        //字符串长度相等, 那么有且只有一处不同
        if(m==n){
            int count = 0;
            for(int i=0; i<n; i++){
                if(s.charAt(i) != t.charAt(i)){
                    count++;
                    if(count>1){
                        return false;
                    }
                }
            }
            return count==1;
        }
        //字符串长度差1, 那么删除长字符串中的某个字符后两个字符串相等
        else{
            //找出更长的字符串, 让s指向更长的字符串
            if(m<n){
                String tmp = s;
                s = t;
                t = tmp;
            }
            //循环终止条件以短的字符串为主
            for(int i=0; i<t.length(); i++){
                if(s.charAt(i)!=t.charAt(i)){
                    //核心: s删除s.charAt(i)之后, 剩余的部分应该和t剩余的部分相等
                    return s.substring(i+1).equals(t.substring(i));
                }
            }
            return true;
        }
    }
}
第一次做; 核心: 1) 使用动态规划找出s和t的编辑距离, 判断是否是1 2) dp[i][j]的含义: 长为i的s和长为j的t的编辑距离
class Solution {
    public boolean isOneEditDistance(String s, String t) {
        int m = s.length(), n = t.length();
        if(Math.abs(m-n)>1){
            return false;
        }
        int[][] dp = new int[m+1][n+1];
        for(int j=1; j<=n; j++){
            dp[0][j] = j;
        }
        for(int i=1; i<=m; i++){
            dp[i][0] = i;
        }

        for(int i=1; i<=m; i++){
            for(int j=1; j<=n; j++){
                if(s.charAt(i-1)==t.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1];
                }else{
                    int tmp = Math.min(dp[i-1][j], dp[i-1][j-1]);
                    tmp = Math.min(tmp, dp[i][j-1]);
                    dp[i][j] = tmp+1;
                }
            }
        }
        return dp[m][n]==1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值