当前位置:网站首页>[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;
}
边栏推荐
- 100 important knowledge points that SQL must master: combining where clauses
- Go从入门到实战——Panic和recover(笔记)
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- Special training of guessing game
- Go from introduction to actual combat - all tasks completed (notes)
- Method of reading file contents by Excel
- Oracle migration MySQL unique index case insensitive don't be afraid
- 神奇的POI读取excel模板文件报错
- CORBA 架构体系指南(通用对象请求代理体系架构)
- JVM memory structure when creating objects
猜你喜欢
随机推荐
STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
win11桌面出现“了解此图片”如何删除
IO stream code
vmware虚拟机PE启动
Common methods of string class
GBase 8a V8版本节点替换期间通过并发数控制资源使用减少对系统影响的方法
uniapp拦截请求
AI 绘画极简教程
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
[LeetCode]动态规划解拆分整数I[Silver Fox]
洛谷P5706 再分肥宅水
[LeetCode]508. 出现次数最多的子树元素和
How to delete "know this picture" on win11 desktop
Codeforces Round #722 (Div. 2)
Codeforces Round #717 (Div. 2)
SQL必需掌握的100个重要知识点:组合 WHERE 子句
Codeforces Round #719 (Div. 3)
Simulink导出FMU模型文件方法









