• 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 }
  • 相关阅读:
    STL中string的源码解读
    Sublime插件:Terminal
    sublime text3安装Package Control
    [转]Sublime Text操作
    python itertools模块实现排列组合
    pandas 选择某几列
    更改pandas dataframe 列的顺序
    pandas之groupby分组与pivot_table透视表
    IPython notebook快捷键(Jupyter notebook)
    人生的意义
  • 原文地址:https://www.cnblogs.com/xinqiwm2010/p/6835523.html
Copyright © 2020-2023  润新知