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. 这个题目思路就是,比较s跟t的长度, 只有abs(s-t) <= 1 才有可能, 然后判断是否只差一个值. 感觉这个题目应该算easy吧. 1. Constraints
1) size >= 0
2) element可以是letter也能是数字 2. Ideas
scan T: O(n) S: O(1) 3. Code
 class Solution:
def oneEdit(self, s,t):
m, n = len(s), len(t)
if abs(m-n) >1 or s ==t: return False
if m > n: s, t, m, n = t, s, n, m
for i in range(m):
if s[i] != t[i]:
return s[i:] == t[i+1:] or s[i+1: ] == t[i+1:] # note s[m:] 并不会报错, 但是s[m]会报错!
return True

[LeetCode] 161. One Edit Distance_Medium的更多相关文章

  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编辑步数为一

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

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

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

  4. [LeetCode#161] One Edit Distance

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

  5. 【LeetCode】161. One Edit Distance

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

  6. 161. One Edit Distance

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

  7. leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)

    https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...

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

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

  9. 【Leetcode】72 Edit Distance

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

随机推荐

  1. Esper学习之八:EPL语法(四)

    关于EPL,已经写了三篇了,预估计了一下,除了今天这篇,后面还有5篇左右.大家可别嫌多,官方的文档对EPL的讲解有将近140页,我已经尽量将废话都干掉了,再配合我附上的例子,看我的10篇文章比那140 ...

  2. * 和-> 优先级

    (Apple *)pf->peel();      则报错说     ct.cpp: In function ‘int main()’:ct.cpp:48: 错误:void 值未如预期地被忽略 ...

  3. win8/win7中使用Git Extensions PuTTy模式提交时 git-credential-winstore.exe": No such file or directory 错误解决方案

    参考:http://www.cnblogs.com/hlizard/p/3627792.html 报错类似以下错误 \"F:/GitExtensions/GitCredentialWinSt ...

  4. sencha touch list更新单行数据

    http://www.cnblogs.com/mlzs/p/3317570.html 如此章所说,点击按钮需要实时更新视图 操作代码如下: onTasteUp: function (list, rec ...

  5. Unity3D笔记 愤怒的小鸟<六> 弹弓发射小鸟

    要实现的目标 实现个性化的鼠标 实现弹弓 选择小鸟.拉升弹弓.发射小鸟 弹弓橡皮筋 声音 1.实现个性化鼠标 效果 2.添加弹弓 建立两个材质 创建一个空GameObject 把两个shoot拖进来统 ...

  6. python的接口和抽象类

    抽象基类 有些面向对象的语言,如JAVA,支持接口,可以声明一个支持给定的一些方法方法,或者支持给定存取协议的类.抽象基类(或者ABCs)是Python里一个相同的特性.抽象基类由abc模块构成,包含 ...

  7. CentOS6.5 下将 Python2.6.6 升级到Python3.5

    一. 从Python官网到获取Python3的包, 切换到目录/usr/local/src #wget https://www.python.org/ftp/python/3.5.1/Python-3 ...

  8. ubuntu16.04安装 lrzsz

    编译安装 root 账号登陆后,依次执行以下命令: tar zxvf lrzsz-.tar.gz cd lrzsz- ./configure make make install 上面安装过程默认把ls ...

  9. 浅谈jvm中的垃圾回收策略

    下面小编就为大家带来一篇浅谈jvm中的垃圾回收策略.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧   java和C#中的内存的分配和释放都是由虚拟机自动管理的,此前我已 ...

  10. Redis经验谈(转)

    原文:http://www.programmer.com.cn/14577/ 新浪作为全世界最大的Redis用户,在开发和运维方面有非常多的经验.本文作者来自新浪,希望能为业界提供一些亲身经历,让大家 ...