原题链接在这里: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. 基于SpringMVC框架项目Demo

    Git地址:https://github.com/JavaWeb1024/SpringMVC 1.     框架简介: 为打造一套集群高可用的框架,集成的技术目前比较成熟,稳定.相关的知识点在网络上也 ...

  2. 移动Web—CSS为Retina屏幕替换更高质量的图片

    来源:互联网 作者:佚名 时间:12-24 10:37:45 [大 中 小] 点评:Retian似乎是屏幕显示的一种趋势,这也是Web设计师面对的一个新挑战;移动应用程序的设计师们已经学会了如何为Re ...

  3. sqlmap我常用到的几个参数

    -r "抓的包存放的文件路径.txt"    一般方便带cookie与session类型 --dbms Oracle/Mysql     指定数据库类型 指定变量注入点变量:在变量 ...

  4. 【液晶模块系列基础视频】5.4.X-GUI字体驱动4

    ============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...

  5. centeros iptable模板文件

    iptables规则是空的.而且他们的selinux是关闭了的,这等同于把系统裸奔(总比windows裸奔好).   使用方法: 1.用root用户登录后 vi /etc/sysconfig/ipta ...

  6. JQuery文件上传插件uploadify在MVC中Session丢失的解决方案

    <script type="text/javascript"> var auth = "@(Request.Cookies[FormsAuthenticati ...

  7. 39. 求分数序列前N项和

    求分数序列前N项和 #include <stdio.h> int main() { int i, n; double numerator, denominator, item, sum, ...

  8. 【翻译】KNACK制作介绍

    KNACK 次世代游戏机的性能开发新世界,PlayStation 4首发游戏的舞台幕后     配合PS4的国内首发,作为SCE的第一个游戏发售的本作. 一边加入发挥次世代机机能的表现,设计了谁都可以 ...

  9. 运维工程师必会的109个Linux命令

    运维工程师必会的109个Linux命令 版本1.0 崔存新 更新于2009-12-26 目录 1 文件管理 6 1.1 basename 6 1.2 cat 6 1.3 cd 7 1.4 chgrp ...

  10. 性能测试工具JMeter

    JMeter介绍     Apache JMeter是Apache组织的开放源代码项目,具有极高的可扩展性,是一个100%纯Java桌面应用,用于压力/性能测试.JMeter可以用于测试静态或者动态资 ...