[LintCode] Edit Distance II

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

edits that can be performed on strings: insert a character, remove a character, or replace a character.

Example

Given s = "aDb", t = "adb"
return true

 

If s is one insertion apart from t, then t is one deletion apart from s as insertion and deletion are reverse 

operations. So we simplify the original problem to given two strings smaller and bigger, with smaller's length

less than or equal to bigger's length, check if smaller is one insertion or replace apart from bigger.

 

Algorithm:

1. From the start of both strings, compare each character. If equal, increment both index pointers by 1; 

2. If not equal: increment the difference count by 1, if this count is > 1, return false; 

                        if the two strings have the same length, then this difference is the only replace that should 

             happen, increment both index pointers and expect no more differences.

                        if they don't have the same length, then it means this difference is where an insertion could happen

                        to make them equal. Only increment bigger's index pointer and expect no more differences.

 

Runtime: O(smaller string's length)

 

 1 public class Solution {
 2     /**
 3      * @param s a string
 4      * @param t a string
 5      * @return true if they are both one edit distance apart or false
 6      */
 7     public boolean isOneEditDistance(String s, String t) {
 8         if(s == null || t == null || Math.abs(s.length() - t.length()) > 1){
 9             return false;
10         }
11         String smaller = s.length() <= t.length() ? s : t;
12         String bigger = s.length() > t.length() ? s : t;
13         int smallerIdx = 0, biggerIdx = 0;
14         int diffCnt = 0;
15         while(smallerIdx < smaller.length()){
16             if(smaller.charAt(smallerIdx) != bigger.charAt(biggerIdx)){
17                 diffCnt++;
18                 if(diffCnt > 1){
19                     return false;
20                 }
21                 if(smaller.length() == bigger.length()){
22                     smallerIdx++;
23                     biggerIdx++;
24                 }
25                 else{
26                     biggerIdx++;
27                 }
28             }
29             else{
30                 smallerIdx++;
31                 biggerIdx++;
32             }
33         }
34         if(smaller.length() == bigger.length() && diffCnt == 0){
35             return false;
36         }
37         return true;
38     }
39 }

 

Related Problems

Edit Distance 

转载于:https://www.cnblogs.com/lz87/p/7052351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值