Edit Distance II
Given two strings S and T, determine if they are both one edit distance apart.
Given s = "aDb", t = "adb"
return true
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的更多相关文章
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)
I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- LintCode Edit Distance
LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
随机推荐
- 雷林鹏分享:jQuery EasyUI 数据网格 - 创建子网格
jQuery EasyUI 数据网格 - 创建子网格 使用数据网格(datagrid)的详细视图,用户可以展开一行来显示附加的详细信息. 任何内容都可以加载作为行详细,子网格也可以动态加载. 本教程将 ...
- Linux中sudo的用法
一.用户在/etc/sudoers文件中的写法语法规则:授权用户 主机=命令动作 这三个要素缺一不可,但在动作之前也可以指定切换到特定用户下,在这里指定切换的用户要用括号括起来,如果不需要密码直接运行 ...
- linux之bash shell
GNU bash ======================================================== 通常计算机硬件是由运算器.控制器.存储器.输入/输出设备等等这些物理 ...
- Confluence 6 教程:空间高手
这个教程将会带你如何在 Confluence 中创建和自定义空间,同时也包括如何删除空间,如果需要的话.通过这个教程,你将成为使用空间的高手. 你需要具有创建空间(Create space)和创建个人 ...
- 启动mysql5.7异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
异常表现 mysql5.7启动时报错 Starting MySQL...The server quit without updating PID file [FAILED]sql/data/insta ...
- scrapy 爬虫框架之持久化存储
scrapy 持久化存储 一.主要过程: 以爬取校花网为例 : http://www.xiaohuar.com/hua/ 1. spider 回调函数 返回item 时 要用y ...
- 23. Merge K Sorted Lists (Java, 归并排序的思路)
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- java多线程wait()方法必须放在while循环里面的原因探析
1.写一个包子生产消费案例:一次生产或消费一个包子,有包子就消费,没有就生产.(部分代码参考传智播客刘意2015Java基础视频讲义) 1.1 写一个Baozi.class,包含main()方法,用来 ...
- hbase的wordcount
package com.neworigin.HBaseMR; import java.io.IOException; import org.apache.hadoop.conf.Configurati ...
- js事件流 事件捕获 及时间冒泡详解
Javascript与HTML之间的交互是通过事件实现. 一.事件流 事件,是文档或浏览器窗口中发生的一些特定的交互瞬间.事件流,描述的是页面中接受事件的顺序.IE9,chrome,Firefox,O ...