当前位置:网站首页>leetCode-面试题 01.05: 一次编辑
leetCode-面试题 01.05: 一次编辑
2022-06-24 09:43:00 【文丑颜不良啊】
题目描述
字符串有三种编辑操作:插入一个英文字符、删除一个英文字符或者替换一个英文字符。 给定两个字符串,编写一个函数判定它们是否只需要一次(或者零次)编辑。
示例
示例 1:
输入:
first = “pale”
second = “ple”
输出: True
示例 2:
输入:
first = “pales”
second = “pal”
输出: False
解题过程
思路及步骤
(1)记 m 为 first 字符串的长度, n 为 second 字符串的长度;
(2)当 |m - n| > 1, 即两个字符串的长度相差大于等于 2 时, 直接 return false, 因为无法通过一次或更少的编辑使得两个串相等;
(3)为了处理方便, 我们需保证 first 字符串为较短的一个, 所以当 second 的长度大于 first 时, 将两字符串交换;
(4)使用 i 遍历 first 字符串, j 遍历 second 字符串, count 用来记录操作次数;
(5)如果 i 和 j 对应的字符相等, 则同时右移;
(6)如果 i 和 j 不相等且 m == n, 这种情况可通过替换操作使两字符相等, 从而保证两字符串相等, 所以 i 和 j 同时右移, 同时 count 自增 1;
(7)如果 i 和 j 不相等且 m ≠ n, 因为我们保证了 first 是较短的一个串, 所以此时 m < n, 这种情况可通过添加或删除操作使两字符相等, 从而保证两字符串相等, 此时, i 不变, j 右移, 同时 count 自增 1;
(8)遍历进行的条件是 i < m && j < n && count <= 1, 循环结束后将 count 是否小于等于 1 的结果返回即可
代码展示
public class OneEditAway {
/** * 记 m 为 first 字符串的长度, n 为 second 字符串的长度 * 当 |m - n| > 1, 即两个字符串的长度相差大于等于 2 时, 直接 return false, 因为无法通过一次或更少的编辑使得两个串相等 * 为了处理方便, 我们需保证 first 字符串为较短的一个, 所以当 second 的长度大于 first 时, 将两字符串交换 * 使用 i 遍历 first 字符串, j 遍历 second 字符串, count 用来记录操作次数 * 如果 i 和 j 对应的字符相等, 则同时右移 * 如果 i 和 j 不相等且 m == n, 这种情况可通过替换操作使两字符相等, 从而保证两字符串相等, 所以 i 和 j 同时右移, 同时 count 自增 1 * 如果 i 和 j 不相等且 m ≠ n, 因为我们保证了 first 是较短的一个串, 所以此时 m < n, 这种情况可通过添加或删除操作使两字符相等, 从而保证两字符串相等, 此时, i 不变, j 右移, 同时 count 自增 1 * 遍历进行的条件是 i < m && j < n && count <= 1, 循环结束后将 count 是否小于等于 1 的结果返回即可 **/
public boolean oneEditAway(String first, String second) {
if (first.length() == 0 || second.length() == 0) {
return true;
}
int m = first.length();
int n = second.length();
if (Math.abs(m - n) > 1) {
return false;
}
if (m > n) {
// 保证 first 是较短的一个
return oneEditAway(second, first);
}
// i 控制 first 的遍历
int i = 0;
// j 控制 second 的遍历
int j = 0;
// count 记录操作次数
int count = 0;
while (i < m && j < n && count <= 1) {
char firstChar = first.charAt(i);
char secondChar = second.charAt(j);
if (firstChar == secondChar) {
// 若两个字符相等, 则同时右移
i++;
j++;
} else {
// 若两个字符不相等
if (m == n) {
// 只能是替换操作
count++;
i++;
j++;
} else {
// m ≠ n, 因为已经确保了 first 为较短的一个串, 所以此时 m < n
// 插入或删除操作
j++;
count++;
}
}
}
return count <= 1;
}
public static void main(String[] args) {
System.out.println(new OneEditAway().oneEditAway("palet", "palt"));
}
}
边栏推荐
- 二叉樹第一部分
- 学习使用KindEditor富文本编辑器,点击上传图片遮罩太大或白屏解决方案
- uniapp实现禁止video拖拽快进
- Binary tree part I
- Which of the top ten securities companies has the lowest Commission and is the safest and most reliable? Do you know anything
- 被困英西中学的师生安全和食物有保障
- SQL Server AVG function rounding
- How large and medium-sized enterprises build their own monitoring system
- 植物生长h5动画js特效
- Nvisual digital infrastructure operation management software platform
猜你喜欢

Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5

Basic operations on binary tree

SVG+js拖拽滑块圆形进度条

Record the range of data that MySQL update will lock

正规方程、、、

411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)

SQL Server AVG函数取整问题

Cicflowmeter source code analysis and modification to meet requirements

一群骷髅在飞canvas动画js特效
随机推荐
Getting user information for applet learning (getuserprofile and getUserInfo)
Development of anti fleeing marketing software for health products
使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
YOLOv6:又快又准的目标检测框架开源啦
植物生长h5动画js特效
整理接口性能优化技巧,干掉慢代码
415-二叉树(144. 二叉树的前序遍历、145. 二叉树的后序遍历、94. 二叉树的中序遍历)
Detailed explanation of PHP singleton mode
SQL Server AVG function rounding
Distributed | how to make "secret calls" with dble
np.float32()
PHP file lock
Operator details
How do novices choose the grade of investment and financial products?
SQL-统计连续N天登陆的用户
Machine learning perceptron and k-nearest neighbor
How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
How to standardize data center infrastructure management process
How does home office manage the data center network infrastructure?
uniapp实现点击拨打电话功能