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. Zabbix安装(debian,centos)

    lnmp和lamp架构搭建一键安装脚本下载地址:https://lnmp.org/download.html  https://github.com/teddysun/lamp/tree/master ...

  2. libavcodec是一款LGPL自由软件编解码库,用于视频和音频数据的编解码工作

    http://zh.wikipedia.org/zh-cn/Libavcodec http://baike.baidu.com/view/856526.htm libavcodec是一款LGPL自由软 ...

  3. Could not allocate 40960 bytes percpu data

    2017-10-01 21:40:56[  176.700091] vif: Could not allocate 40960 bytes percpu data[  263.762812] perc ...

  4. 网络管理命令ping和arping

    ping ping        向目标主机发送icmp请求包 常用来测试当前主机与目标主机网络连接状况 常见选项 -c              设置发包的个数 -s               设 ...

  5. web中集成shiro

    Shiro提供了与Web集成的支持,其通过一个ShiroFilter入口来拦截需要安全控制的URL,然后进行相应的控制,ShiroFilter类似于如Strut2/SpringMVC这种web框架的前 ...

  6. mybatis使用@param("xxx")注解传参和不使用的区别

    public interface SystemParameterMapper { int deleteByPrimaryKey(Integer id); int insert(SystemParame ...

  7. python-flask基本应用模板

    1.模板继承 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  8. Leetcode 1002. 查找常用字符

    1002. 查找常用字符  显示英文描述 我的提交返回竞赛   用户通过次数301 用户尝试次数324 通过次数303 提交次数480 题目难度Easy 给定仅有小写字母组成的字符串数组 A,返回列表 ...

  9. Leetcode 860. 柠檬水找零

    860. 柠檬水找零  显示英文描述 我的提交返回竞赛   用户通过次数187 用户尝试次数211 通过次数195 提交次数437 题目难度Easy 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾 ...

  10. 17. Letter Combinations of a Phone Number C++回溯法

    简单的回溯法! class Solution { public: void backTrack(string digits, vector<string> words, string an ...