原题链接在这里: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. C#设计模式——抽象工厂模式(原文转自:http://blog.jobbole.com/78059/)

    一.引言 在上一专题中介绍了工厂方法模式,工厂方法模式是为了克服简单工厂模式的缺点而设计出来的,简单工厂模式的工厂类随着产品类的增加需要增加额外的代码),而工厂方法模式每个具体工厂类只完成单个实例的创 ...

  2. PixelFormat 像素格式

    enum PixelFormat Pixel formats available for Format7 modes. Enumerator: PIXEL_FORMAT_MONO8  8 bits o ...

  3. 微课程--Android--Android开发学习体系

    四大组件 csdn上面一个关于安卓学习资料的网址 http://blog.csdn.net/vanpersie_9987/article/details/53043590 因为太懒所以花钱买罪受,花了 ...

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

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

  5. centos fastdfs 多服务器 多硬盘 多组 配置详解

    说正文前,先感谢happy_fish100提供的fastdfs,轻量级的分布式文件服务器. 随着用户量的变大,图片,视频等的量会不断的增大,这个时候一个硬盘可能不够用了,就要加硬盘.硬盘加不了时,就要 ...

  6. python 安装pip的时候出现UnicodeEncodeError错误

    另外 Python 中一个比较常见的问题是编码问题,若遇到类似"UnicodeEncodeError: 'ascii' codec can't encode character u'\u62 ...

  7. tunnel.p4

    Tunneling: VXLAN and NVGRE (including L2/L3 Gateway), Geneve, GRE and IPinIP /* Copyright 2013-prese ...

  8. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [2] 首页 APP 接口开发方案 ① 读取数据库方式

    方案一:读取数据库方式 从数据库读取信息→封装→生成接口数据 应用场景: 数据时效性比较高的系统 方案二:读取缓存方式 从数据库获取信息(第一次设置缓存或缓存失效时)→封装(第一次设置缓存或缓存失效时 ...

  9. 超实用PHP函数总结整理

    超实用PHP函数总结整理 2014-12-06    分类:WEB开发.编程开发.首页精华暂无人评论     来源:月光光博客 分享到:更多8 1.PHP加密解密 PHP加密和解密函数可以用来加密一些 ...

  10. ECSHOP二次开发指南

    ECSHOP二次开发指南 发布时间:2013-05-28 12:47:00   来源:   评论:0 点击: 次 [字号:大 中 小] QQ空间新浪微博腾讯微博人人网豆瓣网百度空间百度搜藏开心网复制更 ...