当前位置:网站首页>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"));
}
}
边栏推荐
- Error reading CSV (TSV) file
- Dragging El table sortablejs
- web网站开发,图片懒加载
- 小程序学习之获取用户信息(getUserProfile and getUserInfo)
- 如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
- 记录一下MySql update会锁定哪些范围的数据
- 被困英西中学的师生安全和食物有保障
- 416 binary tree (first, middle and last order traversal iteration method)
- 植物生长h5动画js特效
- PHP uses recursive and non recursive methods to create multi-level folders
猜你喜欢
6.套餐管理业务开发
GeoGebra 实例 时钟
利用pandas读取SQL Sever数据表
二叉树第一部分
Wechat applet learning to achieve list rendering and conditional rendering
How to standardize data center infrastructure management process
How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
Yolov6: the fast and accurate target detection framework is open source
Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction
ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?
随机推荐
解决微信小程序rich-text富文本标签内部图片宽高自适应的方法
Symbol. Iterator iterator
学习使用php对字符串中的特殊符号进行过滤的方法
js代理模式
SQL sever基本数据类型详解
小程序 rich-text中图片点击放大与自适应大小问题
Observer mode
tf.errors
Recursive traversal of 414 binary tree
微信小程序學習之 實現列錶渲染和條件渲染.
canvas无限扫描js特效代码
机器学习——主成分分析(PCA)
静态链接库和动态链接库的区别
PHP file lock
Can the long-term financial products you buy be shortened?
Geogebra instance clock
Floating point notation (summarized from cs61c and CMU CSAPP)
利用pandas读取SQL Sever数据表
416-二叉树(前中后序遍历—迭代法)
Engine localization adaptation & Reconstruction notes