当前位置:网站首页>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"));
}
}
边栏推荐
- Getting user information for applet learning (getuserprofile and getUserInfo)
- TP5 using post to receive array data times variable type error: solution to array error
- Programming questions (continuously updated)
- El table Click to add row style
- SQL sever基本数据类型详解
- SSH Remote Password free login
- Top issue tpami 2022! Behavior recognition based on different data modes: a recent review
- Regular matching mailbox
- 414-二叉树的递归遍历
- YOLOv6:又快又准的目标检测框架开源啦
猜你喜欢

大中型企业如何构建自己的监控体系

有关二叉树 的基本操作

How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!

Use of vim

Cookie encryption 4 RPC method determines cookie encryption

CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算

Canvas draw picture

Three ways to use applicationcontextinitializer

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

Operator details
随机推荐
js代理模式
PHP encapsulates a file upload class (supports single file and multiple file uploads)
机器学习——主成分分析(PCA)
数组无缝滚动demo
SQL Sever中的窗口函数row_number()rank()dense_rank()
Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
canvas无限扫描js特效代码
How large and medium-sized enterprises build their own monitoring system
微信小程序学习之 实现列表渲染和条件渲染.
编程题(持续更新)
Why is JSX syntax so popular?
How do novices choose the grade of investment and financial products?
414-二叉树的递归遍历
SQL Server AVG function rounding
Using pandas to read SQL server data table
Which of the top ten securities companies has the lowest Commission and is the safest and most reliable? Do you know anything
操作符详解
uniapp实现禁止video拖拽快进
形状变化loader加载jsjs特效代码
自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏