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 }
  • 相关阅读:
    Oracle数据类型
    Windows10安装node.js,vue.js以及创建第一个vue.js项目
    Redis Desktop Manager安装
    VMware安装Linux系统
    前端入门学习路线
    架构
    HTML
    Oracle修改表类型方法
    Python学习
    Python(字符串和编码)
  • 原文地址:https://www.cnblogs.com/xinqiwm2010/p/6835523.html
Copyright © 2011-2022 走看看