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 ...
随机推荐
- 如何模拟一个http请求并把response的内容保存下载下来,导出到excel中(结尾福利)
def doExport(self): # 模拟一个http请求 url = u'%s?dumptype=investigation&dumpid=%s&timezone=8' % ( ...
- JFinal3.0 sql管理与动态生成
原文: 本节学习目标主要是使用JFinal中自带的Template Engin来实现对sql的管理.JFinal中为sql管理提供了3个指令#sql.#para.#namespace,来增强sql功能 ...
- boke练习: @PreAuthorize authentication.name.equals的数据绑定与验证
boke练习: @PreAuthorize authentication.name.equals的数据绑定与验证 先看2个简单的例子: 一 @PostMapping("/{username} ...
- xml ----> 几个常用dtd头文件模板
环境: idea ce 2018.1 "File --> settings... --> Editor --> file and code templates" ...
- every day a practice —— morning(4)
If there’s one thing New Yorkers love more than discovering a new secret remedy, it’s telling other ...
- 老老实实学WCF
老老实实学WCF 第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型,包括定义和实现服务协定.配置服务.寄宿服务.通过添加服务引用的方式配置客户端并访问服务.我 ...
- appium+python+unittest+HTMLRunner编写UI自动化测试集
简介 获取AppPackage和AppActivity 定位UI控件的工具 脚本结构 PageObject分层管理 HTMLTestRunner生成测试报告 启动appium server服务 以py ...
- python生成随机整数
python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成 ...
- jquery选择器和基本操作
定义:jQuery 对象就是通过jQuery包装DOM对象后产生的对象.jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用 jQuery 里的方法: ...
- ConcurrentHashMap1.8源码解析
深入并发包 ConcurrentHashMap 概述 JDK1.8的实现已经摒弃了Segment的概念,而是直接用Node数组+链表+红黑树的数据结构来实现,并发控制使用Synchronized和CA ...