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. js -history.back(-1)和history.go(-1) 区别

    既然history.back(-1)和history.go(-1)都是返回之前页面,   history.back(-1)//直接返回当前页的上一页,,是个新页面   history.go(-1)// ...

  2. 8张思维导图学习javascript

    分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...

  3. [namespace]PHP命名空间的使用基础

    -------------------------------------------------------------------------------------------------- 一 ...

  4. 让一个非窗口组件(non-windowed component)可以接受来自Windows的消息

    为什么要这样做? 有时候我们需要一个非窗口组件(比如一个非继承自TWinContrl的组件)可以接受Windows消息.要接受消息就需要一个窗口句柄,但是非窗口组件却没有句柄.这篇文章将讲述怎么让一个 ...

  5. 创建DLL动态链接库——模块定义法(def)

    DLL模块定义法(Module-Definition File,即DEF):在VS家族IDE中,根据提示新增.def文件,如下: LIBRARY 关键字; mytestDll 库名; DLL_ADD ...

  6. 远程批量查看windosws操作系统3389端口的开放情况

    本文只提供思想.具体可以根椐情况拓展. 前提是需要配置远程主机的SNMP协议.主要是共同体哟. 脚本使用: 1.拷贝check_tcp到脚本执行的主机中或在此主机中安装nagios; 2.保持list ...

  7. ArcGIS 10安装及破解

    1.下载 ArcGIS 10 安装程序及破解文件后面提供电驴的下载地址(可以使用迅雷.QQ旋风等下载工具下载),下载文件是一个光盘镜像文件:? ArcGIS_Desktop10_122519.iso. ...

  8. unity 三种注入示例

    /* * 演示Unity 注入 * */ using Microsoft.Practices.Unity; using System; namespace Unity.Property.Inject ...

  9. 吴裕雄 python 熵权法确定特征权重

    一.熵权法介绍 熵最先由申农引入信息论,目前已经在工程技术.社会经济等领域得到了非常广泛的应用. 熵权法的基本思路是根据各个特征和它对应的值的变异性的大小来确定客观权重. 一般来说,若某个特征的信息熵 ...

  10. 学JS的心路历程-闭包closure

    闭包是是纯函式语言的一个特性,也是JS的一个关键性的特色,虽然不了解也能开发程序,但我们不是这种人对吧? 闭包不仅可以减少某些高阶功能的代码数量和复杂度,并且可以让我们做到原本无法做的复杂功能.听到这 ...