zoukankan      html  css  js  c++  java
  • Edit Distance II

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

    Example

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

    思维惯性造成上来就想call Edit Distance的算法 然后看需要改多少步
    后来想想这个问题“One”很特殊 要好好利用 才发现简单的string compare就可以解决
    最后判断前面的字符全部相等的情况,此时只看长度
     
     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         // Write your code here
     9         if(s==null||t==null) return true;
    10         if(s!=null&&t==null||s==null&&t!=null) return false;
    11         int sLen = s.length();
    12         int tLen = t.length();
    13         if(Math.abs(sLen-tLen)>=2) return false;
    14         
    15         for(int i=0; i<Math.min(sLen, tLen);i++){
    16             if(s.charAt(i) != t.charAt(i)){
    17                 if(sLen==tLen ){
    18                     return s.substring(i+1, sLen).equals(t.substring(i+1, tLen));
    19                 } else if (sLen< tLen ){
    20                     return s.substring(i, sLen).equals(t.substring(i+1, tLen));
    21                 } else if (sLen> tLen ){
    22                     return s.substring(i+1, sLen).equals(t.substring(i, tLen));
    23                 }
    24             }
    25         }
    26         if(sLen==tLen){
    27             return false;
    28         }else{
    29             return true;
    30         }
    31     }
    32 }
  • 相关阅读:
    学习计划 23月
    bash学习笔记
    bash 中 while读取文件并通过 ssh执行命令出现的问题及解决方法
    bash 学习笔记2
    fedora 启动 openssh
    lesson5 键盘的应用
    第十三章 int指令
    第十五章 外中断
    第十二章 内中断
    第十四章 端口
  • 原文地址:https://www.cnblogs.com/xinqiwm2010/p/6835523.html
Copyright © 2011-2022 走看看