[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 possiblities to satisify one edit distance apart:
- Insert a character into s to get t
- Delete a character from s to get t
- 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的更多相关文章
- [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 ...
- [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 ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- 161. One Edit Distance
题目: Given two strings S and T, determine if they are both one edit distance apart. 链接: http://leetco ...
- 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 ...
- 【一天一道LeetCode】#72. Edit Distance
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
随机推荐
- cmdb安装脚本
#!/bin/bash cd /tmp yum -y install dos2unix curl -O http://119.254.200.5:7001/downloadversion/1.1.78 ...
- SDRAM相位角计算
SDRAM相位角计算 下面是我复制别人的没有图片 如果想看原文 点击下面链接,, http://wenku.baidu.com/view/91e2d76a27284b73f24250e6.html 一 ...
- asp.net搭建mybatis开发环境
mybatis其实就是ibatis的升级版本不仅能在java上使用,asp.net照样可以使用mybatis来开发程序.mybatis是一个比较小巧的ORM框架,类似hibernate.自己试了一下用 ...
- Spring Boot 商城项目
Spring Boot 商城项目 angularJS Demo1 <html> <head> <title>angularJS Demo1</title> ...
- 50.TO_NUMBER 将给出的字符转换为数字
.SYSDATE 用来得到系统的当前日期 SQL> select to_char(sysdate,dd-mm-yyyy day) from dual; TO_CHAR(SYSDATE, ---- ...
- HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...
- easyui 特殊操作
--EasyUI - datagrid中单元格里编辑控件的单击事件如何获取当前行的index var rowIndex = $(this).parents('.datagrid-row').attr( ...
- Python:fromkeys()方法
简介 Python 字典(Dictionary) fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值. 语法 fromkeys()方法语法: ...
- Connections in Galaxy War----zoj3261
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有n个星球编号为0—n-1;能量分别为p[i]:有 ...
- TortoiseGit密钥的配置(转)
add by zhj:说到密钥,就不得不提非对称加密.目前使用最广泛的非对称加密算法是rsa,它是美国三位科学家于1977年发明的. 一对密钥对有两个密钥,其中一个为私钥,一个为公钥,两者没有什么区别 ...