当前位置:网站首页>【日常训练】面试题 01.05. 一次编辑
【日常训练】面试题 01.05. 一次编辑
2022-05-14 13:51:09【Puppet__】
题目
字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串,编写一个函数判定它们是否只需要一次(或者零次)编辑。
示例 1:
输入:
first = “pale”
second = “ple”
输出: True
示例 2:
输入:
first = “pales”
second = “pal”
输出: False
代码
package dayLeetCode;
public class dayleetcode0105 {
public boolean oneEditAway(String first, String second) {
boolean flag = true;
// len的取值只能是-1 0 1
int len = second.length() - first.length();
if (len > 1 || len < -1){
return false;
}
int i = 0, j = 0;
// 记录编辑次数
int cnt = 0;
while (i < first.length() && j < second.length()){
if (first.charAt(i) != second.charAt(j)){
// 进行了插入、删除、或者替换操作
// 插入
if (len == 1){
cnt++;
// 先减后加保证i不变
i--;
}else if(len == 0){
// 替换
cnt++;
} else if (len == -1){
// 删除
cnt++;
j--;
}
}
if (cnt > 1){
flag = false;
break;
}
i++;
j++;
}
return flag;
}
public static void main(String[] args) {
dayleetcode0105 obj = new dayleetcode0105();
System.out.println(obj.oneEditAway("pale", "ple"));
}
}
边栏推荐
猜你喜欢
随机推荐
- 大学英语六级考试(CET6)历年阅读试题译文
- CET-6 六级考试必备范文10篇
- 大学英语六级作文模板大全(完整版)
- 超实用25个英语演讲口语金句
- cet4-6高频词汇
- 新iPhone到手后要开启这8个设置,让手机更安全更好用
- 是能力更是文化,谈谈IT系统的安全发布
- tensorflow学习6 -- 跑通UNet图像分割
- postman循环调用同一个接口
- (Transfer Learning and fine tuning)迁移学习与微调
- ICDAR 2021竞赛 科学文献分析——表格识别综述部分(剩余部分是文档布局分析)
- Webpach打包器的如何使用
- 2021-IEEE论文-深度神经网络在文档图像表格识别中的应用现状及性能分析
- Why can 128 KB soul duel achieve such a long plot?
- The price rise twice a year highlights the greed of the introduction of Jidian, and the global chip hopes to find another way out
- Second week project training report
- Third week project training report
- Fourth week project training report
- Arm is still brilliant, but it is already under siege
- Performance hit everyone, Foxconn and apple are still close, and Lixun precision seems to have failed to take advantage of the opportunity
- 5g fell from the altar. Compared with 4G, the available technology is limited, and the only advantage is that it is faster
- A new force in the field of HPC -- Fu force supercomputing
- Data center white paper (2022): the data center industry continues to upgrade and fully enables the digital economy
- Error reading registry by C WPF application
- Teach you how to do prototype design
- P4 learning - Basic forwarding
- The contents of the input box are displayed on the right
- Clickhouse 22.3 lts release
- Programmer flirting special ~ ~ ~ nice H5 cube creative photo album, resources free!!! A gift from a programmer to a girl is very suitable for a young lady!
- [missing scan tool] awvs, appscan download and installation (with network disk link)
- 5.3 binary tree_ Code implementation of optimized heap and Top-k problem
- 5.4 binary tree_ Code implementation of various traversal and calculation
- Record: com mysql. cj. jdbc. exceptions. CommunicationsException: Communications link failure... [effective through personal test]
- Record: 1221 - incorrect usage of Union and order by [effective through personal test]
- [force deduction] backtracking 1 - Foundation + combination
- What role does cloud computing play in building intelligence?
- Abstract - the shortest novel of 2016
- Fiddler packet capture guide 05: breaking points
- EDA technology and market analysis
- 是能力更是文化,談談IT系統的安全發布