博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode - One Edit Distance
阅读量:4947 次
发布时间:2019-06-11

本文共 970 字,大约阅读时间需要 3 分钟。

题目:

One Edit Distance

Given two strings S and T, determine if they are both oneedit distance apart.

Hint:

1. If | n – m | is greater than 1, we know immediately both are not one-editdistance apart.
2. It might help if you consider these cases separately, m == n and m ≠ n.
3. Assume that m is always ≤ n, which greatly simplifies the conditionalstatements. If m > n, we could just simply swap S and T.
4. If m == n, it becomes finding if there is exactly one modified operation. Ifm ≠ n, you do not have to consider the delete operation. Just consider theinsert operation in T.

bool OneEditDistance(const string &s1, const string &s2){	if (s1.size() > s2.size())		return OneEditDistance(s2, s1);	if (s2.size() - s1.size()>1)		return false;	int i1 = 0, i2 = 0;	while (i1 < s1.size())	{		if (s1[i1] == s2[i2])		{			++i1;			++i2;		}		else			break;	}	if (i1 == s1.size())		return true;	++i2;	while (i1 < s1.size() && i2

转载于:https://www.cnblogs.com/mengfanrong/p/5204537.html

你可能感兴趣的文章
Note of introduction of Algorithms (Lecture 1)
查看>>
CentOS 7 安装 建立svn仓库 远程连接
查看>>
Maven 创建动态web 3.0项目
查看>>
CodeforcesD Bubble Sort Graph
查看>>
现代文经典
查看>>
CentOS7 PostgreSQL 主从配置( 二)
查看>>
事务的ACID特性
查看>>
网络拥堵造成数据库性能表现异常的问题排查
查看>>
.NET文档生成工具ADB[更新至2.3]
查看>>
CentOS下编译安装Busybox
查看>>
Python3入门(十三)——连接数据库
查看>>
从程序员到项目经理(五):不是人人都懂的学习要点
查看>>
range用法
查看>>
2.27
查看>>
第6章第2讲循环嵌套结构
查看>>
cordova(phonegap)+qjm 一统天下
查看>>
安卓Drawable——Shape
查看>>
集合 LinkedList、ArrayList、Set、Treeset
查看>>
Python 发邮件
查看>>
关于js事件执行顺序
查看>>