当前位置:网站首页>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;
}
}边栏推荐
- Knowledge points of mall development
- 力扣解法汇总944-删列造序
- PHP security development 13 column module of blog system
- Linux (centos7) installer mysql - 5.7
- How to improve the advertising rating of advertising, that is, the quality score?
- 力扣解法汇总1022-从根到叶的二进制数之和
- Force deduction solution summary 396 rotation function
- Summary of force deduction solution 427- establishment of quadtree
- Metaverse × How will smart cities develop?
- 2022西式面点师(技师)复训题库及在线模拟考试
猜你喜欢

阿里云oss文件上传系统

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

ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法

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

Smartbi helps you solve the problem of losing high-value customers

Don't miss it! Five large data visualization screens that HR must collect

Ozzanmation action system based on SSE

matplotlib. pyplot. Bar chart (II)

消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)

Graphic data analysis | data cleaning and pretreatment
随机推荐
MySQL高级部分知识点
Graphical data analysis | data analysis tool map
Graphic data analysis | business cognition and data exploration
力扣解法汇总699-掉落的方块
力扣解法汇总面试题 17.11-单词距离
Linux(CentOS6)安装MySQL5.5版本数据库
力扣解法汇总732-我的日程安排表 III
[untitled]
力扣解法汇总824-山羊拉丁文
The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
leetcodeSQL:612. Nearest distance on plane
力扣解法汇总436-寻找右区间
How to stop anti-virus software from blocking a web page? Take gdata as an example
Add sequence number column to MySQL query result set
力扣解法汇总467-环绕字符串中唯一的子字符串
2022最全面的Redis事务控制(带图讲解)
力扣解法汇总875-爱吃香蕉的珂珂
PHP security development 13 column module of blog system
The establishment and introduction of the announcement module of PHP development blog system
Force deduction solution summary 386 dictionary order