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就可以解决
最后判断前面的字符全部相等的情况,此时只看长度
 
 public class Solution {
/**
* @param s a string
* @param t a string
* @return true if they are both one edit distance apart or false
*/
public boolean isOneEditDistance(String s, String t) {
// Write your code here
if(s==null||t==null) return true;
if(s!=null&&t==null||s==null&&t!=null) return false;
int sLen = s.length();
int tLen = t.length();
if(Math.abs(sLen-tLen)>=2) return false; for(int i=0; i<Math.min(sLen, tLen);i++){
if(s.charAt(i) != t.charAt(i)){
if(sLen==tLen ){
return s.substring(i+1, sLen).equals(t.substring(i+1, tLen));
} else if (sLen< tLen ){
return s.substring(i, sLen).equals(t.substring(i+1, tLen));
} else if (sLen> tLen ){
return s.substring(i+1, sLen).equals(t.substring(i, tLen));
}
}
}
if(sLen==tLen){
return false;
}else{
return true;
}
}
}

Edit Distance II的更多相关文章

  1. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  2. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

  3. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  4. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  5. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  7. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  8. LintCode Edit Distance

    LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...

  9. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

随机推荐

  1. MySQL official tutorial

    1.installation 2.setup environment variables add %/MySQL Server/bin to path. then restart cmd/powers ...

  2. spring cloud: Hystrix(七):Hystrix的断容器监控dashboard

    Hystrix的断容器监控dashboard. dashboard是用来监控Hystrix的断容器监控的,图形化dashboard是如何实现指标的收集展示的. dashboard 本地端口8730 项 ...

  3. English trip V1 - B 16. Giving Reasons 提供个人信息 Teacher:Lamb Key: Why/Because

    In this lesson you will learn how to give reasons for something you've done. 课上内容(Lesson) Why do peo ...

  4. 20181013xlVba年级成绩报表

    Public Sub 高一成绩报表() Application.ScreenUpdating = False Application.DisplayAlerts = False Application ...

  5. calc_load

    http://www.penglixun.com/tech/system/how_to_calc_load_cpu.html #define FSHIFT 11 /* nr of bits of pr ...

  6. MyEclipse6.5的SVN插件的安装

    在线安装 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→Find and Install; 2. 选择Search for new features to i ...

  7. apiCloud 浏览图片

    点击链接查看api详情 https://docs.apicloud.com/Client-API/Func-Ext/photoBrowser var photoBrowser = api.requir ...

  8. 【PowerDesigner】【1】简单介绍

    正文: 创建表格 File→New Model→(Model types; Physical Data Model; Physical Diagram)Model name:名称:DBMS:数据库类型 ...

  9. 用vivado实现4比特加法器

    `timescale 1ns / 1ps module add_4_beha( a, b, cin, sum ); :] a; :] b; input cin; output sum; :]a; :] ...

  10. 笔记react router 4(一)

    用过react router4.X的小伙伴一定知道,比起3.X的版本,router的使用上有了很大的改变. 首先,我们只需要安装 react-router-dom 即可使用.看到“dom”想必你就该知 ...