原题链接在这里: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:

  1. Insert a character into s to get t
  2. Delete a character from s to get t
  3. 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;
}
}

类似Edit Distance.

LeetCode One Edit Distance的更多相关文章

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

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

  2. 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 ...

  3. [Leetcode Week8]Edit Distance

    Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...

  4. [LeetCode] 72. Edit Distance 编辑距离

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

  5. 【leetcode】Edit Distance

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

  6. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

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

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

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

  8. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  9. 【leetcode】Edit Distance (hard)

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

随机推荐

  1. SSH全注解开发

    web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&quo ...

  2. php中alert弹出时单双引号问题

    php代码中单双引号问题是个很重要的问题,使用不当会造成很多麻烦.先记录一下今天写alert遇到的麻烦.我做登录的时候,成功时想弹出个提示说登录成功.写alert语句时不显示.其实就是单双引号弄得不对 ...

  3. Ubuntu 启动停止脚本

    /etc/init.d 目录下的开机启动脚本 1. more redis_8010 #/bin/sh #Configurations injected by install_server below. ...

  4. css 细节收集

    细节1……………………………………………………………………………… 一.当文字与图片在一行,需要将文字与图片底对齐,需要这样写: <li>记住密码<img src="&qu ...

  5. 嵌入式 如何定位死循环或高CPU使用率(linux) 及性能优化

    嵌入式 如何定位死循环或高CPU使用率(linux) ln -s /mnt/nfs/_install/usr/bin/sort /usr/bin/sort awk '{print $1,$2,$14, ...

  6. linux下mysql定时备份数据库

    linux下mysql定时备份数据库 (2010-10-21 12:40:17) 转载▼ 标签: 杂谈   一.用命令实现备份 首页进入mysql的bin目录 1.备份数据#mysqldump -uu ...

  7. 实用Javascript代码片段

    清除select下拉选项,添加并选择特点选项 $('#mySelect') .find('option') .remove() .end() .append('<option value=&qu ...

  8. javaWeb中struts开发——helloworld

    1.新建一个web项目 2.选中project,右键,选择MyElcipse,选择add  struts capab...添加struts支持,然后自己命名包 3.Struts在建立jsp时,标签要到 ...

  9. session配置理解

    session.cache_limiter 指定会话页面所使用的缓冲控制方法,默认为nocache.session.cache_expire 以分钟数指定缓冲的会话页面的存活期,默认为180.此设定对 ...

  10. Diode

    Diode https://zh.wikipedia.org/wiki/真空管 抽真空 电子在于其放射过程中,因会与空气中之组成分子相撞而产生阻力,因此电子经由如空气之类的介质来移动的话,将会比在真空 ...