LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/
题目:
Given two strings s and t, determine if they are both one edit distance apart.
Note:
There are 3 possiblities to satisify one edit distance apart:
- Insert a character into s to get t
- Delete a character from s to get t
- Replace a character of s to get t
Example 1:
Input: s = "ab", t = "acb"
Output: true
Explanation: We can insert 'c' into s to get t.
Example 2:
Input: s = "cab", t = "ad"
Output: false
Explanation: We cannot get t from s by only one step.
Example 3:
Input: s = "1203", t = "1213"
Output: true
Explanation: We can replace '0' with '1' to get t.
题解:
若是长度相差大于1, return false. 若是长度相差等于1, 遇到不同char时, 长的那个向后挪一位. 若是长度相等, 遇到不同char时同时向后挪一位.
出了loop还没有返回,就是到目前为止都相同,那么看长度差是不是等于1. 这里等于0表示完全相同也不可以.
Time Complexity: O(Math.min(len1, len2)).
Space: O(1).
AC Java:
public class Solution {
public boolean isOneEditDistance(String s, String t) {
if(s == null || t == null){
return false;
}
int len1 = s.length();
int len2 = t.length();
for(int i = 0; i < Math.min(len1, len2); i++){
if(s.charAt(i) != t.charAt(i)){
if(len1 == len2){
return s.substring(i+1).equals(t.substring(i+1));
}else if(len1 > len2){
return s.substring(i+1).equals(t.substring(i));
}else{
return s.substring(i).equals(t.substring(i+1));
}
}
}
return Math.abs(len1-len2) == 1;
}
}
LeetCode One Edit Distance的更多相关文章
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- Java for LeetCode 072 Edit Distance【HARD】
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- tomcat6.0安装
tomcat的安装基本是一步一步按提示来就行了: 这里填写用户名和密码,自己一定要记住啊,下面是路径的选择 然后查看安装成功与否,打开浏览器输入 然后看到下面页面就可以了
- 个人翻译的cedec2010基于物理的光照
作为自己介绍基于物理渲染计划的一部分,在自己总结和发布的同时,也会翻译一些国外的优秀资料做推广 本文是Tri Ace 在 cedec2010上发布的文章,主要描述了他们基于物理光照的实现方法,这 ...
- linux下mysql定时备份数据库
linux下mysql定时备份数据库 (2010-10-21 12:40:17) 转载▼ 标签: 杂谈 一.用命令实现备份 首页进入mysql的bin目录 1.备份数据#mysqldump -uu ...
- spotlight监控工具使用
利用spotlight工具可以监控如下系统: 1.Spotlight on Unix 监控Linux服务器 1)安装 Spotlight on Unix 2)配置spotlight登陆用 ...
- Virtual Memory DEMAND PAGING - The avoidance of thrashing was a major research area in the 1970s and led to a vari- ety of complex but effective algorithms.
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION With the use of pagin ...
- Scripting Languages
Computer Science An Overview _J. Glenn Brookshear _11th Edition A subset of the imperative programmi ...
- Machine Learning in Action – PCA和SVD
降维技术, 首先举的例子觉得很好,因为不知不觉中天天都在做着降维的工作 对于显示器显示一个图片是通过像素点0,1,比如对于分辨率1024×768的显示器,就需要1024×768个像素点的0,1来表示, ...
- ArcGIS Server 缓存服务增加新比例尺缓存
win10 + Server 10.4 + ArcMap 10.4 操作简单说明: ①窗口上方Customize栏→Toolbars→ Customize→ 搜索到 manege map serv ...
- ArcMap 标注、注记、图形文本
标注.注记.图形文本 2016年8月10日10:29 ArcMap中怎样向地图添加文本,其中标注与注记是重点内容,此处对此进行总结. 参考链接: ①地图文本基本词汇: 什么是文本? ArcGIS 提供 ...
- 【转】说说如何使用unity Vs来进行断点调试
大家可以从这下载最新版的unity vs. UnityVs1.81下载 1. 安装unity vs.首先我们打开我们下载的unity vs.然后就会看见里面有3个文件,我们双击UnityVS 2 ...