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

思路

此题可算是[leetcode]72. Edit Distance 最少编辑步数的一个拆分简化版本

代码

 class Solution {
public boolean isOneEditDistance(String s, String t) {
int m = s.length(), n = t.length();
if(m == n) return isOneModified(s, t);
if(m - n == 1) return isOneDeleted(s, t);
if(n - m == 1) return isOneDeleted(t, s);
// 长度差距大于2直接返回false
return false;
} private boolean isOneModified(String s, String t){
boolean modified = false;
// 看是否只修改了一个字符
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) != t.charAt(i)){
if(modified) return false;
modified = true;
}
}
return modified;
} public boolean isOneDeleted(String longer, String shorter){
// 找到第一组不一样的字符,看后面是否一样
for(int i = 0; i < shorter.length(); i++){
if(longer.charAt(i) != shorter.charAt(i)){
return longer.substring(i + 1).equals(shorter.substring(i));
}
}
return true;
}
}

[leetcode]161. One Edit Distance编辑步数为一的更多相关文章

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

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  2. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  3. [LeetCode#161] One Edit Distance

    Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...

  4. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

  5. 161. One Edit Distance

    题目: Given two strings S and T, determine if they are both one edit distance apart. 链接: http://leetco ...

  6. 【Leetcode】72 Edit Distance

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

  7. [LeetCode] 161. One Edit Distance_Medium

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  8. [LC] 161. One Edit Distance

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  9. 【一天一道LeetCode】#72. Edit Distance

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

随机推荐

  1. DATASNAP远程方法返回TSTREAM正解(转咏南兄)

    DATASNAP远程方法返回TSTREAM正解 DATASNAP远程方法返回TSTREAM,如果数据大小超过32K是会报错的.许多DELPHIER栽在这个上头,甚至开始怀疑TSTREAM返回数据的可行 ...

  2. 使用支持向量机(SVM) 算法进行分类

    1 支持向量机(SVM)的基本概念   SVM是一种分类算法,其侧重于模式识别方面.使用SVM可以大大提高分类的准确性.   分类相当于模式识别的子集合,模式识别重点在于对已知数据进行特征发现与提取. ...

  3. 一个神奇的BUG :Failed to finalize session : INSTALL_FAILED_INVALID_APK: /data/app/vmdl99393454.tmp/10_slice__ signatures are inconsistent

    Android Studio 在Gradle编译完成后安装APK时总是失败,EventLog提示如下信息: Failed to finalize session : INSTALL_FAILED_IN ...

  4. 一个关于EasyUI超恶心的BUG。。。Cannot read property 'options' of undefined

    控制台Console抛出的异常: jquery.easyui.min.js:9148 Uncaught TypeError: Cannot read property 'options' of und ...

  5. IIS8.0 部署WCF Services

    今天在Win 8的IIS上部署WCF Services,访问SVC文件时出现找不到处理程序的错误,以前遇到这个问题时都是尝试通过注册asp.net的方式处理一下,但是在Win8下这招不灵了,出现如下提 ...

  6. spring boot 事务支持

  7. js高级-数组的map foreach 方法

    函数式编程  sort map forEach  ....  jQuery() 数组的sort 方法 传入一个匿名函数 就是函数式编程 ie9 以上的浏览器支持 map 方法 map方法 返回一个新数 ...

  8. 终止执行js的方法

    (一)在function里面 (1)return;(2)return false; (二)非function方法里面 alert("before error.");throw Sy ...

  9. ECharts-初始化方法参数不能传入jquery对象

    ECharts-初始化方法参数不能传入jquery对象

  10. sqlite 时间戳转时间

    ), 'unixepoch','localtime') from messages where data != '' order by timestamp desc 官方eg: Examples Co ...