当前位置:网站首页>[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;
}
边栏推荐
- Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
- Process control task
- Go从入门到实战——接口(笔记)
- 强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
- CEPH distributed storage
- Method of reading file contents by Excel
- 今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
- [LeetCode]30. 串联所有单词的子串
- Set code exercise
猜你喜欢
随机推荐
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
SQL必需掌握的100个重要知识点:IN 操作符
Null pointer exception
抖音的兴趣电商已经碰到流量天花板?
Educational Codeforces Round 108 (Rated for Div. 2)
OpenSSL 编程 一:基本概念
oracle迁移mysql唯一索引大小写不区分别怕
Go从入门到实战——多态(笔记)
Scrum和看板的区别
Codeforces Round #717 (Div. 2)
Go从入门到实战——依赖管理(笔记)
语言弱点列表--CWE,一个值得学习的网站
linux下安装oracle11g 静默安装教程
100 important knowledge points that SQL must master: filtering data
Let Ma Huateng down! Web3.0, hopeless
Installing Oracle11g under Linux
[LeetCode]513. 找树左下角的值
100 important knowledge points that SQL must master: creating calculation fields
SQL必需掌握的100个重要知识点:组合 WHERE 子句
excel读取文件内容方法









