当前位置:网站首页>Force deduction solution summary interview question 01.05 Edit once
Force deduction solution summary interview question 01.05 Edit once
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/one-away-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Length of judgement , If second and first The same can only be a replacement . Less than, it can only be deleted , If it is greater than, it can only be plus . * Try three possibilities , If none of the three is possible, return false. * Then we can simplify the scene , Adding and deleting actually belong to the same scenario , If you reduce one character first Can be converted to second, that second Adding a character can also be converted to first. * So we just need to consider reducing first Just a string scene . If second Than first Long , Then the two exchange .
Code :
public class SolutionMST0105 {
public boolean oneEditAway(String first, String second) {
char[] firstChars = first.toCharArray();
char[] secondChars = second.toCharArray();
int num = firstChars.length - second.length();
int diffNum = 0;
if (num == 0) {
for (int i = 0; i < firstChars.length; i++) {
if (firstChars[i] != secondChars[i]) {
diffNum++;
}
}
return diffNum <= 1;
}
if (Math.abs(num) > 1) {
return false;
}
if (secondChars.length > firstChars.length) {
char[] local = secondChars;
secondChars = firstChars;
firstChars = local;
}
for (int i = 0; i < firstChars.length; i++) {
int secondIndex = i - diffNum;
if (secondIndex < 0 || secondIndex >= secondChars.length) {
diffNum++;
} else if (firstChars[i] != secondChars[secondIndex]) {
diffNum++;
}
if (diffNum > 1) {
return false;
}
}
return true;
}
}边栏推荐
- Is the bidding price fixed for each click?
- How should programmers solve the problem of buying vegetables? Take you hand in hand to quickly order and grab vegetables by using the barrier free auxiliary function
- 力扣解法汇总436-寻找右区间
- Navicat for MySQL 11 Linux 破解方法
- 力扣解法汇总1728-猫和老鼠 II
- Design principle [Demeter's Law]
- 力扣解法汇总821-字符的最短距离
- Implementation scheme of iteration and combination pattern for general tree structure
- Pagination writing of PHP security open 10 article list module
- 决定广告质量的三个主要因素
猜你喜欢

MySQL advanced knowledge points

Explore performance optimization! Performance improvement from 2 months to 4 hours!

商城开发知识点

UE4\UE5触摸屏touch事件:单指、双指

Why do we use Google search ads?

竞价广告每次点击出价多少钱是固定的吗?

The establishment and introduction of the announcement module of PHP development blog system

Basedexclassloader

Alicloud OSS file upload system

消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
随机推荐
el-upload上传文件
php开发09 文章模块的删除和文章分类编写
Pagination writing of PHP security open 10 article list module
入手Ticwatch2
Oracle 11g graphic download installation tutorial (step by step)
MySQL advanced knowledge points
通过搜索广告附加信息让广告更具相关性
力扣解法汇总944-删列造序
Almost all schools will ask for the second round exam! Come in and recite the answer!
How to improve the advertising rating of advertising, that is, the quality score?
Invert words in a string (split, double ended queue)
How can low code platforms improve cost effectiveness?
Metaverse × How will smart cities develop?
Is the bidding price fixed for each click?
xcall 集群脚本(查看jps命令)
国资入股,建业地产这回稳了吗?
How to automatically color cells in Excel
Don't miss it! Five large data visualization screens that HR must collect
ozzanimation-基於sse的動作系統
力扣解法汇总433-最小基因变化