当前位置:网站首页>[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;
}
边栏推荐
- Acwing周赛57-数字操作-(思维+分解质因数)
- ∫(0→1) ln(1+x) / (x ² + 1) dx
- Codeforces Round #722 (Div. 2)
- Icml2022 | scalable depth Gaussian Markov random field
- gomock mockgen : unknown embedded interface
- What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
- STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
- Go from introduction to practice - polymorphism (note)
- Go从入门到实战——依赖管理(笔记)
- Quick excel export
猜你喜欢

ICML2022 | 可扩展深度高斯马尔可夫随机场

Go从入门到实战—— 多路选择和超时控制(笔记)

Go from introduction to actual combat - package (notes)

图解基于AQS队列实现的CountDownLatch和CyclicBarrier

What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?

SQL必需掌握的100个重要知识点:排序检索数据

.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法

Go from introduction to practice - Interface (notes)

Codeforces Round #717 (Div. 2)

Go from introduction to practice -- coordination mechanism (note)
随机推荐
Is it safe to open an account and buy stocks? Who knows
uniapp拦截请求
[LeetCode]动态规划解分割数组I[Red Fox]
Quick excel export
Go 访问GBase 8a 数据库的一个方法
SQL必需掌握的100个重要知识点:组合 WHERE 子句
Process control task
Icml2022 | scalable depth Gaussian Markov random field
Go from entry to practice - multiple selection and timeout control (notes)
数组作业题
io流代码
SQL必需掌握的100个重要知识点:检索数据
Day8 ---- 云资讯项目介绍与创建
Common methods of string class
让马化腾失望了!Web3.0,毫无希望
Bit. Store: long bear market, stable stacking products may become the main theme
PCIE知识点-008:PCIE switch的结构
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
Go从入门到实战——仅执行一次(笔记)
Method of reading file contents by Excel