• [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 

  • 相关阅读:
    新人手册
    使用koa-mysql-session时报错
    自建windows服务器如何部署egg应用
    mac os 10.15 virtualBox6.0.12崩溃
    thinkphp3.2 上传图片兼容小程序
    PHP版本微信支付开发
    php mysql 按照指定年月查找数据 数据库create_time为时间戳
    Mac OS 查看端口和杀死进程
    MAC PHP7 如何disable xdebug
    TCP为什么是三次握手,为什么不是两次或者四次 && TCP四次挥手
  • 原文地址:https://www.cnblogs.com/lz87/p/7052351.html
Copyright © 2020-2023  润新知