[LC] 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.
class Solution {
public boolean isOneEditDistance(String s, String t) {
if (Math.abs(s.length() - t.length()) > 1) {
return false;
}
if (s.length() == t.length()) {
return isOneModify(s, t);
} else if (s.length() > t.length()) {
return isOneDel(s, t);
} else {
return isOneDel(t, s);
}
}
private boolean isOneModify(String s, String t) {
int diff = 0, i = 0;
while (i < s.length()) {
if (s.charAt(i) != t.charAt(i)) {
diff += 1;
}
i += 1;
}
return diff == 1;
}
private boolean isOneDel(String s, String t) {
int i = 0;
for (; i < s.length() && i < t.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
break;
}
}
return s.substring(i + 1).equals(t.substring(i));
}
}
[LC] 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. 给定两个字符串,判断他们是否是一步变换得到 ...
- 161. One Edit Distance
题目: Given two strings S and T, determine if they are both one edit distance apart. 链接: http://leetco ...
- [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 ...
- [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] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- HDU_2604 矩阵快速幂 较难推的公式
一个排队问题,f代表女,m代表男,f和m出现的几率相等.问一个长为L的队伍不能出现 fmf 和 fff这样的串总共有多少种. 这个题目的公式递推略难啊...我看了别人博客才想明白原来是这么递推出来的. ...
- CPU压力测试--限制到指定范围
作用:增加CPU使用率到指定范围 1.书写shell脚本增加CPU压力 #! /bin/bash # filename cputest.sh endless_loop() { echo -ne &qu ...
- 协程与异步IO
协 程 协程,又称微线程,纤程. 协程的优势: 协程的特点在于是一个线程执行. 协程的最大优势是极高的执行效率,因为子程序切换不是线程切换,而是由 ...
- python pandas 画图、显示中文、股票K线图
目录: 1.pandas官方画图链接 2.标记图中数据点 3.画图显示中文 4.画股票K线图 5.matplotlib基本用法 6.format输出 6.format输出例子 eps_range=[0 ...
- Python D9 学习
Python 设置环境 当安装好Python 后 在计算机的属性里面 高级语言设置 环境变量. 环境变量里面的path 更改为Python的 树目录 可以从计算机直接下达命令 打开Pytho ...
- elasticsearch-集群管理
1.集群结构 ES 通常以集群方式工作.以提高搜索性能.容错能力.高可用.实现PB级数据搜索. 相关概念: (1)结点: ES集群由多台ES服务器组成.每个ES 服务端就是个一个NODE结点 (2)分 ...
- Linux(CENTOS7) Tomcat服务成功发布但局域网浏览器无法访问
问题 : 我在linux搭建了一个tomcat服务器,tomcat开启后,发现在局域网浏览器上无法访问该tomcat,浏览器报无法访问服务器错误,我查看了tomcat的日志,路径..../tomcat ...
- [Java-基础] 什么是ORM
ORM简介 ORM:对象关系映射:Object Relational Mapping 用于实现面向对象编程语言里不同类型系统的数据之间的转换 一般的,数据库绝大部分是面向关系的数据库,但是写代码的 ...
- Dynamics CRM - 利用 JavaScript 打开指定 entity 的新建窗口并传递需要的参数
由于业务逻辑需要,需要从某个 Entity 的 Record 中弹出其他 Entity 的创建窗口,并将当前 Entity 中的某些值传递到新打开的窗口中,具体的 JS 代码如下: //定义一个参数对 ...
- 6.react 基础 - 关于 react 开发 的原则
1. 声明式开发 通过绑定元素 在数据变更时 对元素进行动态渲染 2. 可以与其他框架并存 不在React的绑定元素内, 可以使用其他框架 如 ( vue jQuery 等 ) 进行元素操作 3. 组 ...