边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance
要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator)
- 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录mismatch个数,err:0,err:1,有一位mismatch,这时候再if/else分replace or ins,err:2:return False
- https://repl.it/CquW
- 错误点:index回移的logic:短的回移,因为每步必向前,所以相当于短的不动,长的动
- 未知长度:因为两个string之间char的差异最多1,所以state可以用三个变量表示:replace,ins_a,ins_b。
- 程序结构就是loop在外,然后if/else(if就是直接判断replace/ins_a/ins_b),注意这里不是按cur_a==cur_b区分。而是三个变量都是False,那么直接比较cur_a/cur_b,否则有一个True,那么就需要错位比较,然后重新把三个变量中,另外如果这个分支上再三个都是False,那说明选不可能了,return False而不是继续下一个循环。
- 循环之后?分三种情况(a多,b多 or 一样)。a多,可以是没有diff(这样多出的一个是insert的),or 有diff但是错位一直相同并且到了终点。
- 注意最后err可能在最后,类似merge,要考虑a.hasNext or b.hasNext
- https://repl.it/CsNV
- 错误点:
- 如果相同返回False,所以最后相同长度的case只能是replace为True
- 这题要用到loop的末值i,而python的loop最后的i值不是过界的,所以要用while loop
# Given two strings S and T, determine if they are both one edit distance apart.
# Hide Company Tags Snapchat Uber Facebook Twitter
# Hide Tags String
# Hide Similar Problems (H) Edit Distance
class Solution(object):
def isOneEditDistance(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if abs(len(s)-len(t))>1:
return False
error = 0
i,j = 0, 0
ns, nt = len(s), len(t)
while i<ns and j<nt:
if s[i]!=t[j]:
error+=1
if error>1:
return False
if ns>nt:
j-=1 # error: should be j-=1
elif ns<nt:
i-=1
i+=1
j+=1
return error==1 or (error==0 and ns!=nt)
sol = Solution()
assert sol.isOneEditDistance("ab","cab")==True
# Given two strings S and T, determine if they are both one edit distance apart.
# Hide Company Tags Snapchat Uber Facebook Twitter
# Hide Tags String
# Hide Similar Problems (H) Edit Distance
class Solution(object):
def isOneEditDistance(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
ns, nt = len(s), len(t)
if abs(ns-nt)>1:
return False
diff, replace, ins_s, ins_t = False, False, False, False
pre = -1
i=0
while i<len(s) and i<len(t):
if not replace and not ins_s and not ins_t:
if s[i]!=t[i]:
diff=replace=ins_s=ins_t=True
else:
if ins_s and t[i]!=s[pre]: ins_s=False
if ins_t and s[i]!=t[pre]: ins_t=False
if replace and t[i]!=s[i]: replace=False
if not (ins_s or ins_t or replace): return False
pre = i
i+=1
if i<len(t):
return not diff or ((t[i]==s[pre] and ins_s) and i+1==len(t))
elif i<len(s):
return not diff or ((s[i]==t[pre] and ins_t) and i+1==len(s))
else:
return replace
sol = Solution()
assert sol.isOneEditDistance("c", "c")==False
assert sol.isOneEditDistance("a", "ba")==True
边工作边刷题:70天一遍leetcode: day 71-2的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 71
Longest Substring with At Most Two Distinct Characters # Given a string, find the length of the long ...
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
随机推荐
- Docker on CentOS for beginners
Introduction The article will introduce Docker on CentOS. Key concepts Docker Docker is the world's ...
- MyEclipse+Mysql (二)
上一节介绍了如何在Myeclipse中连接mysql 这一节介绍如何在java程序中访问mysql数据库中的数据b并进行简单的操作 创建一个javaProject,并输入如下java代码: packa ...
- 【iOS】Quartz2D信纸条纹
一.前导程序 新建一个项目,在主控制器文件中实现以下几行代码,就能轻松的完成图片在视图中的平铺. - (void)viewDidLoad { [super viewDidLoad]; UIImage ...
- xmapp的安装
搭建网站常识性的你首先得搭建一个服务器. 首先APACHE是世界使用排名第一的WEB服务器软件,但是安装APACHE WEB服务器并不容易.如果你想添加MYSQL.PHP和PERL,那就更难了.所以可 ...
- 2013 最新的 play web framework 版本 1.2.3 框架学习文档整理
Play framework框架学习文档 Play framework框架学习文档 1 一.什么是Playframework 3 二.playframework框架的优点 4 三.Play Frame ...
- 挖掘机技术哪家强(c++实现)
描述:为了用事实说明挖掘机技术到底哪家强,组织一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入:输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位参赛者的 ...
- CSS 去掉inline-block元素间隙的几种方法
最近做移动端页面时,经常会用到inline-block元素来布局,但无可避免都会遇到一个问题,就是inline-block元素之间的间隙.这些间隙会导致一些布局上的问题,需要把间隙去掉.对于inlin ...
- sublimeText插件推荐
工欲善其事必先利其器.sublimeText是前端开发工程师的一把利器,它的优点包含: 随时保留文件的修改 Goto Anything,智能搜索; 简单全面的插件体系; 代码地图; 快速启动 ... ...
- Access sql语句创建表及字段类型
创建一张空表: Sql="Create TABLE [表名]" 创建一张有字段的表: Sql="Create TABLE [表名]([字段名1] MEMO NOT NUL ...
- 实践GTD三周年后的体会
从2011年7月开始实践GTD到现在已经3年多了,按照年初设定的目标,应该写点GTD的体会,但提起笔来的时候却发现并没有什么太新的体会,大部分体会都已经在以前的文章中写到了,甚至有些流程已经简化了,看 ...