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:

  1. Input: s = "ab", t = "acb"
  2. Output: true
  3. Explanation: We can insert 'c' into s to get t.

Example 2:

  1. Input: s = "cab", t = "ad"
  2. Output: false
  3. Explanation: We cannot get t from s by only one step.

Example 3:

  1. Input: s = "1203", t = "1213"
  2. Output: true
  3. Explanation: We can replace '0' with '1' to get t.

题目

给定两个字符串,判断其编辑步数是否为1

思路

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

代码

  1. class Solution {
  2. public boolean isOneEditDistance(String s, String t) {
  3. int m = s.length(), n = t.length();
  4. if(m == n) return isOneModified(s, t);
  5. if(m - n == 1) return isOneDeleted(s, t);
  6. if(n - m == 1) return isOneDeleted(t, s);
  7. // 长度差距大于2直接返回false
  8. return false;
  9. }
  10.  
  11. private boolean isOneModified(String s, String t){
  12. boolean modified = false;
  13. // 看是否只修改了一个字符
  14. for(int i = 0; i < s.length(); i++){
  15. if(s.charAt(i) != t.charAt(i)){
  16. if(modified) return false;
  17. modified = true;
  18. }
  19. }
  20. return modified;
  21. }
  22.  
  23. public boolean isOneDeleted(String longer, String shorter){
  24. // 找到第一组不一样的字符,看后面是否一样
  25. for(int i = 0; i < shorter.length(); i++){
  26. if(longer.charAt(i) != shorter.charAt(i)){
  27. return longer.substring(i + 1).equals(shorter.substring(i));
  28. }
  29. }
  30. return true;
  31. }
  32. }

[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. ReentrantLock 学习笔记

    有篇写的很不错的博客:https://blog.csdn.net/aesop_wubo/article/details/7555956    基于JDK1.8 参考着看源码 ,弄清楚lock()和un ...

  2. b2BuoyancyController 使用浮力

    package{ import Box2D.Collision.b2AABB; import Box2D.Collision.b2RayCastInput; import Box2D.Collisio ...

  3. web前端基础知识!

    [HTML文档的基本结构和语法][基本结构]: <HTML> HTML 文件开始 <HEAD> HTML 文件的头部开始 <title> 网页的标题</tit ...

  4. C# windows服务:C#windows服务中的Timer控件的使用

    C# windows服务程序中的Timer控件的使用问题是如何解决的呢? 今天和同事一起研究了下C# windows服务程序中的Timer控件的使用的写法. 我们在建立一个C# windows服务程序 ...

  5. iOS修改状态栏颜色

    application.statusBarStyle = .LightContent // 在APPlication中设置全局状态栏颜色,为白色 application.statusBarHidden ...

  6. 解决idea控制台乱码及项目乱码

    如果控制台出现乱码,解决办法: 第1方案:.找到安装idea的路径下找idea文件下的bin中vmoptions文件,打开该文件,加上-Dfile.encoding=UTF-8 第二方案: 第3种方案 ...

  7. 吴裕雄 python 机器学习-DMT(1)

    import numpy as np import operator as op from math import log def createDataSet(): dataSet = [[1, 1, ...

  8. django中使用mysql数据库的事务

    django中怎么使用mysql数据库的事务   Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起 ...

  9. java开始的笔记

    这几天第一写java的代码有些东西不是很明白: java的输入跟c/c++有点不一样,他的输入都是要首先创建每一个变量的内存,并且输入的类型不同. 就像string的那样. Scanner s=new ...

  10. mysql创建用户和库

    先用root登陆mysql,然后运行下面代码创建mysql用户和库 下面创建的帐号:test123  密码:123456  库名:test123 CREATE USER '; GRANT USAGE ...