当前位置:网站首页>[daily training] interview question 01.05 One edit
[daily training] interview question 01.05 One edit
2022-05-14 13:57:41【Puppet__】
subject
There are three editing operations for Strings : Insert a character 、 Delete or replace a character . Given two strings , Write a function to determine if they only need to be done once ( Or zero times ) edit .
Example 1:
Input :
first = “pale”
second = “ple”
Output : True
Example 2:
Input :
first = “pales”
second = “pal”
Output : False
Code
package dayLeetCode;
public class dayleetcode0105 {
public boolean oneEditAway(String first, String second) {
boolean flag = true;
// len The value of can only be -1 0 1
int len = second.length() - first.length();
if (len > 1 || len < -1){
return false;
}
int i = 0, j = 0;
// Record the number of edits
int cnt = 0;
while (i < first.length() && j < second.length()){
if (first.charAt(i) != second.charAt(j)){
// Inserted 、 Delete 、 Or replace operation
// Insert
if (len == 1){
cnt++;
// First reduce and then add guarantee i unchanged
i--;
}else if(len == 0){
// Replace
cnt++;
} else if (len == -1){
// Delete
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系統的安全發布