当前位置:网站首页>Leetcode interview question 01.05: primary editing
Leetcode interview question 01.05: primary editing
2022-06-24 10:15:00 【Ugly and ugly】
Title Description
There are three editing operations for Strings : Insert an English character 、 Delete an English character or replace an English character . Given two strings , Write a function to determine if they only need to be done once ( Or zero times ) edit .
Example
Example 1:
Input :
first = “pale”
second = “ple”
Output : True
Example 2:
Input :
first = “pales”
second = “pal”
Output : False
The problem solving process
Ideas and steps
(1) remember m by first Length of string , n by second Length of string ;
(2) When |m - n| > 1, That is, the length difference between two strings is greater than or equal to 2 when , direct return false, Because it is impossible to make two strings equal with one or less edits ;
(3) For convenience , We need to make sure first The string is a shorter one , So when second Is longer than first when , Swap two strings ;
(4) Use i Traverse first character string , j Traverse second character string , count Used to record the number of operations ;
(5) If i and j The corresponding characters are equal , Move right at the same time ;
(6) If i and j Not equal and m == n, In this case, the two characters can be made equal by the replacement operation , So as to ensure that the two strings are equal , therefore i and j Move right at the same time , meanwhile count Self increasing 1;
(7) If i and j Not equal and m ≠ n, Because we promised first Is a shorter string , So at this time m < n, In this case, the two characters can be made equal by adding or deleting , So as to ensure that the two strings are equal , here , i unchanged , j Move right , meanwhile count Self increasing 1;
(8) The condition for traversal is i < m && j < n && count <= 1, At the end of the cycle count Less than or equal to 1 Just return the result
Code display
public class OneEditAway {
/** * remember m by first Length of string , n by second Length of string * When |m - n| > 1, That is, the length difference between two strings is greater than or equal to 2 when , direct return false, Because it is impossible to make two strings equal with one or less edits * For convenience , We need to make sure first The string is a shorter one , So when second Is longer than first when , Swap two strings * Use i Traverse first character string , j Traverse second character string , count Used to record the number of operations * If i and j The corresponding characters are equal , Move right at the same time * If i and j Not equal and m == n, In this case, the two characters can be made equal by the replacement operation , So as to ensure that the two strings are equal , therefore i and j Move right at the same time , meanwhile count Self increasing 1 * If i and j Not equal and m ≠ n, Because we promised first Is a shorter string , So at this time m < n, In this case, the two characters can be made equal by adding or deleting , So as to ensure that the two strings are equal , here , i unchanged , j Move right , meanwhile count Self increasing 1 * The condition for traversal is i < m && j < n && count <= 1, At the end of the cycle count Less than or equal to 1 Just return the result **/
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) {
// Guarantee first Is the shorter one
return oneEditAway(second, first);
}
// i control first The traversal
int i = 0;
// j control second The traversal
int j = 0;
// count Record the number of operations
int count = 0;
while (i < m && j < n && count <= 1) {
char firstChar = first.charAt(i);
char secondChar = second.charAt(j);
if (firstChar == secondChar) {
// If two characters are equal , Move right at the same time
i++;
j++;
} else {
// If two characters are not equal
if (m == n) {
// It can only be a replacement operation
count++;
i++;
j++;
} else {
// m ≠ n, Because it has been ensured that first For a shorter string , So at this time m < n
// Insert or delete operation
j++;
count++;
}
}
}
return count <= 1;
}
public static void main(String[] args) {
System.out.println(new OneEditAway().oneEditAway("palet", "palt"));
}
}
边栏推荐
- 记录一下MySql update会锁定哪些范围的数据
- leetCode-2221: 数组的三角和
- 411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
- Can the long-term financial products you buy be shortened?
- Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.
- 3.员工的增删改查
- MYSQL数据高级
- 解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
- leetCode-1089: 复写零
- Getting user information for applet learning (getuserprofile and getUserInfo)
猜你喜欢
H5网页如何在微信中自定义分享链接
操作符详解
How does home office manage the data center network infrastructure?
JS singleton mode
Yolov6: the fast and accurate target detection framework is open source
记录一下MySql update会锁定哪些范围的数据
Record the range of data that MySQL update will lock
微信小程序学习之 实现列表渲染和条件渲染.
Queue queue
Troubleshooting steps for Oracle pool connection request timeout
随机推荐
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
leetCode-1089: 复写零
canvas掉落的小球重力js特效动画
Use of vim
4.分类管理业务开发
CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算
uniapp实现点击拨打电话功能
np.float32()
有关二叉树 的基本操作
numpy.linspace()
Floating point notation (summarized from cs61c and CMU CSAPP)
学习使用php对字符串中的特殊符号进行过滤的方法
leetCode-面试题 01.05: 一次编辑
小程序学习之获取用户信息(getUserProfile and getUserInfo)
Observer mode
6.套餐管理业务开发
Regular matching mailbox
PHP uses recursive and non recursive methods to create multi-level folders
numpy.logical_or
Machine learning - principal component analysis (PCA)