161. One Edit Distance

本文探讨了如何判断两个字符串是否仅相差一个编辑距离。通过比较字符串的不同字符位置,实现O(n)的时间复杂度和O(1)的空间复杂度。

题目:

Given two strings S and T, determine if they are both one edit distance apart.

链接: http://leetcode.com/problems/one-edit-distance/

题解:

求两个字符串是否只有1个Edit Distance。 看着这道题又想起了Edit Distance那道。不过这道题不需要用DP,只用设一个boolean变量hasEdited来逐字符判断就可以了。写法大都借鉴了曹神的代码。用短的string和长的比较,假如字符不同,则hasEdited为true,假如s比t短,则下标i退回1来继续比较insert / delete的case。否则比较的是replace。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public boolean isOneEditDistance(String s, String t) {  // compare short string with long string
        if(s == null || t == null)
            return false;
        if(s.length() > t.length())
            return isOneEditDistance(t, s);
        if(t.length() - s.length() > 1)
            return false;
        boolean hasEdited = false;
        
        for(int i = 0, j = 0; i < s.length(); i++, j++) {           // detect if only 1 change need to be made
            if(s.charAt(i) != t.charAt(j)) {
                if(hasEdited)
                    return false;
                hasEdited = true;
                if(s.length() < t.length())                         //if s.length() < t.length(), back up one letter and continue compare
                    i--;
            }
        }
        
      return hasEdited || (s.length() < t.length());    // (s.length() < t.length()) for insert case or delete case
    }
}

 

Update:

把s.equals(t)的相等判断从尾部挪到头部了,这样尾部直接return true就可以了

public class Solution {
    public boolean isOneEditDistance(String s, String t) {
        if(s == null || t == null || s.equals(t)) 
            return false;
        if(s.length() > t.length())
            return isOneEditDistance(t, s);
        if(t.length() - s.length() > 1)
            return false;
        
        boolean hasEdited = false;
        
        for(int i = 0, j = 0; i < s.length(); i++, j++) {
            if(s.charAt(i) != t.charAt(j)) {
                if(hasEdited)
                    return false;
                hasEdited = true;
                if(s.length() < t.length())
                    i--;
            }
        }
        
        return true;
    }
}

 

二刷:

依然是曹神的解法。

Java:

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public boolean isOneEditDistance(String s, String t) {
        if (s == null || t == null || s.equals(t) || Math.abs(s.length() - t.length()) > 1) return false;
        if (s.length() > t.length()) return isOneEditDistance(t, s);
        boolean hasDiff = false;
        for (int i = 0, j = 0; i < s.length(); i++, j++) {
            if (s.charAt(i) != t.charAt(j)) {
                if (hasDiff) return false;
                hasDiff = true;
                if (s.length() < t.length()) i--;
            }
        }
        return true;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值