[leetcode]161. One Edit Distance编辑步数为一
Given two strings s and t, determine if they are both one edit distance apart.
Note:
There are 3 possiblities to satisify one edit distance apart:
- Insert a character into s to get t
- Delete a character from s to get t
- Replace a character of s to get t
Example 1:
- Input: s = "ab", t = "acb"
- Output: true
- Explanation: We can insert 'c' into s to get t.
Example 2:
- Input: s = "cab", t = "ad"
- Output: false
- Explanation: We cannot get t from s by only one step.
Example 3:
- Input: s = "1203", t = "1213"
- Output: true
- Explanation: We can replace '0' with '1' to get t.
题目
给定两个字符串,判断其编辑步数是否为1
思路
此题可算是[leetcode]72. Edit Distance 最少编辑步数的一个拆分简化版本
代码
- class Solution {
- public boolean isOneEditDistance(String s, String t) {
- int m = s.length(), n = t.length();
- if(m == n) return isOneModified(s, t);
- if(m - n == 1) return isOneDeleted(s, t);
- if(n - m == 1) return isOneDeleted(t, s);
- // 长度差距大于2直接返回false
- return false;
- }
- private boolean isOneModified(String s, String t){
- boolean modified = false;
- // 看是否只修改了一个字符
- for(int i = 0; i < s.length(); i++){
- if(s.charAt(i) != t.charAt(i)){
- if(modified) return false;
- modified = true;
- }
- }
- return modified;
- }
- public boolean isOneDeleted(String longer, String shorter){
- // 找到第一组不一样的字符,看后面是否一样
- for(int i = 0; i < shorter.length(); i++){
- if(longer.charAt(i) != shorter.charAt(i)){
- return longer.substring(i + 1).equals(shorter.substring(i));
- }
- }
- return true;
- }
- }
[leetcode]161. One Edit Distance编辑步数为一的更多相关文章
- [LeetCode] 161. One Edit Distance 一个编辑距离
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- 161. One Edit Distance
题目: Given two strings S and T, determine if they are both one edit distance apart. 链接: http://leetco ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- [LeetCode] 161. One Edit Distance_Medium
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- [LC] 161. One Edit Distance
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- 【一天一道LeetCode】#72. Edit Distance
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
随机推荐
- ReentrantLock 学习笔记
有篇写的很不错的博客:https://blog.csdn.net/aesop_wubo/article/details/7555956 基于JDK1.8 参考着看源码 ,弄清楚lock()和un ...
- b2BuoyancyController 使用浮力
package{ import Box2D.Collision.b2AABB; import Box2D.Collision.b2RayCastInput; import Box2D.Collisio ...
- web前端基础知识!
[HTML文档的基本结构和语法][基本结构]: <HTML> HTML 文件开始 <HEAD> HTML 文件的头部开始 <title> 网页的标题</tit ...
- C# windows服务:C#windows服务中的Timer控件的使用
C# windows服务程序中的Timer控件的使用问题是如何解决的呢? 今天和同事一起研究了下C# windows服务程序中的Timer控件的使用的写法. 我们在建立一个C# windows服务程序 ...
- iOS修改状态栏颜色
application.statusBarStyle = .LightContent // 在APPlication中设置全局状态栏颜色,为白色 application.statusBarHidden ...
- 解决idea控制台乱码及项目乱码
如果控制台出现乱码,解决办法: 第1方案:.找到安装idea的路径下找idea文件下的bin中vmoptions文件,打开该文件,加上-Dfile.encoding=UTF-8 第二方案: 第3种方案 ...
- 吴裕雄 python 机器学习-DMT(1)
import numpy as np import operator as op from math import log def createDataSet(): dataSet = [[1, 1, ...
- django中使用mysql数据库的事务
django中怎么使用mysql数据库的事务 Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起 ...
- java开始的笔记
这几天第一写java的代码有些东西不是很明白: java的输入跟c/c++有点不一样,他的输入都是要首先创建每一个变量的内存,并且输入的类型不同. 就像string的那样. Scanner s=new ...
- mysql创建用户和库
先用root登陆mysql,然后运行下面代码创建mysql用户和库 下面创建的帐号:test123 密码:123456 库名:test123 CREATE USER '; GRANT USAGE ...