当前位置:网站首页>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"));
}
}
边栏推荐
- Troubleshooting steps for Oracle pool connection request timeout
- 有关二叉树 的基本操作
- How to manage massive network infrastructure?
- Regular matching mailbox
- SQL Server AVG函数取整问题
- Producer / consumer model
- numpy.logical_and()
- 如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
- Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
- Cicflowmeter source code analysis and modification to meet requirements
猜你喜欢

numpy.linspace()

有关二叉树 的基本操作

Nvisual digital infrastructure operation management software platform

Wechat applet learning to achieve list rendering and conditional rendering

机器学习——主成分分析(PCA)

SQL Server AVG函数取整问题

微信小程序學習之 實現列錶渲染和條件渲染.

Go language development environment setup +goland configuration under the latest Windows

TP5 using post to receive array data times variable type error: solution to array error

Basic operations on binary tree
随机推荐
413 binary tree Foundation
上升的气泡canvas破碎动画js特效
Detailed explanation of PHP singleton mode
SQL Sever中的窗口函数row_number()rank()dense_rank()
JS proxy mode
学习使用php对字符串中的特殊符号进行过滤的方法
414-二叉树的递归遍历
Basic operations on binary tree
Machine learning perceptron and k-nearest neighbor
机器学习——主成分分析(PCA)
uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
SQL-统计连续N天登陆的用户
微信小程序学习之 实现列表渲染和条件渲染.
numpy.logical_or
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
Phpstrom code formatting settings
Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
CICFlowMeter源码分析以及为满足需求而进行的修改