美文网首页
One Edit Distance

One Edit Distance

作者: 瞬铭 | 来源:发表于2020-07-07 14:10 被阅读0次

    https://leetcode.com/problems/one-edit-distance/
    给定word1,word2 计算从是否可以一步从word1变到word2
    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.

    Edit Distance比较类似,但是不用那么复杂把最小Distance求出,只用遵循规则计算是否一步能到

        public Boolean isOneEditDistance(String s, String t) {
            int sLen = s.length();
            int tLen = t.length();
    
            if ((Math.abs(sLen - tLen) > 1)) {
                return false;
            }
    
            if (sLen > tLen) {
                return isOneEditDistance(s.substring(0, sLen - 1), t);
            } else if (sLen < tLen) {
                return isOneEditDistance(s, t.substring(0, t.length() - 1));
            }
            return s.equals(t) ? true : isOneEditDistance(s.substring(0, sLen - 1), t.substring(0, t.length() - 1));
        }
    

    相关文章

      网友评论

          本文标题:One Edit Distance

          本文链接:https://www.haomeiwen.com/subject/somaqktx.html