当前位置:网站首页>[LeetCode]161. Edit distance of 1
[LeetCode]161. Edit distance of 1
2022-06-27 21:50:00 【A Fei algorithm】
subject
Given two strings s and t, Judge whether their editing distance is 1.
Be careful :
The editing distance is equal to 1 There are three possible situations :
Go to s Insert a character in to get t
from s Delete a character to get t
stay s Replace a character in to get t
Examples
Example 1:
Input : s = "ab", t = "acb"
Output : true
explain : Can be 'c' Insert string s To get t.
Example 2:
Input : s = "cab", t = "ad"
Output : false
explain : Unable to get 1 Step operation s Turn into t.
Example 3:
Input : s = "1203", t = "1213"
Output : true
explain : You can string s Medium '0' Replace with '1' To get t.
Method 1: Compare strings
public static void main(String[] args) {
_1st handler = new _1st();
String s = "ab", t = "acb";
Assert.assertTrue(handler.isOneEditDistance(s, t));
s = "cab";
t = "ad";
Assert.assertFalse(handler.isOneEditDistance(s, t));
s = "1203";
t = "1213";
Assert.assertTrue(handler.isOneEditDistance(s, t));
}
public boolean isOneEditDistance(String s, String t) {
int sn = s.length(), tn = t.length();
// maintain s The length of is less than t
if (sn > tn) {
return isOneEditDistance(t, s);
}
if (tn - sn > 1) return false;// Greater than 1 when , return false
for (int i = 0; i < sn; i++) {
if (s.charAt(i) != t.charAt(i)) {
//s And t The same length , Compare the following
if (sn == tn) {
return s.substring(i + 1).equals(t.substring(i + 1));
} else {
//s And t The length is different s The characters of are short
return s.substring(i).equals(t.substring(i + 1));
}
}
}
return sn + 1 == tn;
}
边栏推荐
猜你喜欢

Knowledge sorting of exception handling

100 important knowledge points that SQL must master: using functions to process data

100 important knowledge points that SQL must master: sorting and retrieving data

畅游动态规划之区间DP

Go from introduction to practice - Interface (notes)

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions

Codeforces Round #719 (Div. 3)

Codeforces Global Round 14

Go从入门到实战——channel的关闭和广播(笔记)

ICML2022 | 可扩展深度高斯马尔可夫随机场
随机推荐
∫(0→1) ln(1+x) / (x² + 1) dx
抖音的兴趣电商已经碰到流量天花板?
GBase 8a OLAP函数group by grouping sets的使用样例
有时间看看ognl表达式
Method of reading file contents by Excel
Have time to look at ognl expressions
SQL必需掌握的100个重要知识点:IN 操作符
Go from introduction to practice - Interface (notes)
Go from introduction to actual combat -- channel closing and broadcasting (notes)
专题教程——选队长游戏
100 important knowledge points that SQL must master: sorting and retrieving data
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Go from introduction to actual combat - context and task cancellation (notes)
CORBA 架构体系指南(通用对象请求代理体系架构)
Day8 ---- 云资讯项目介绍与创建
洛谷P5706 再分肥宅水
[LeetCode]508. 出現次數最多的子樹元素和
io流代码
Oracle migration MySQL unique index case insensitive don't be afraid
Go from entry to practice - multiple selection and timeout control (notes)