[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 ...
随机推荐
- JavaScript 之 Function
JavaScript function 语句定义和用法: function 语句用于声明一个函数. 函数声明后,我们可以在需要的时候调用. 在 JavaScript 中,函数是对象,函数也有属性和方法 ...
- 一维消消乐(DP)
一维消消乐是一款非常简单的游戏.有n颗珠子排成一排,每一颗珠子有一个价值w(可能是负数). 游戏是这样,你可以选择如若干对相邻的珠子,让他们同时消去.每一对珠子的消失,都会使得总分数加上两颗珠子相乘的 ...
- Java--HashMap排序
package connection; import java.util.Collections; import java.util.Comparator; import java.util.Hash ...
- JavaScript—瀑布流
现在网页中需要翻页的列表,好多都已经改为瀑布流了.所以这个思路还是特别重要的 HTML Css 页面 因为每个图片的高度不一样所以她的 top 和left 我们待会通过JS计算 动态生成 js部分 思 ...
- 使用idea出现的错误
错误:打开maven项目时出现"程序包 com.sun.org.apache.xpath.internal 不可见 "的错误 这个问题出现的原因是: jdk版本的问题.可能是因为有 ...
- OpenMP笔记(二)
原文:https://www.bearoom.xyz/2019/02/18/openmp2/ OpenMP是由三部分组成的:指令.库函数和环境变量. 一.指令 在C/C++中使用OpenMP需要用到的 ...
- php 使用redis队列简单实用
简介:队列要遵守先进先出的原则 demo.php <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $arr = ...
- MVC思想概叙
随着应用系统的逐渐增大,系统的业务逻辑复杂度是以几何的方式增长,在这种情况下,如果依然把所有的业务逻辑都放在JSP页面中,那将成为一场恶梦. MVC思想将应用中各个组件按照功能来进行分类,不同的组将使 ...
- win32框架
win32的框架 1.入口函数 2.窗口注册类信息 3.窗口创建 4.显示窗口 5.更新窗口 6.消息循环 7.入口函数结束 WNDCLASSEX wcex;窗口类结构 wcex.cbSize = s ...
- ORB-SLAM2的编译运行以及TUM数据集测试
ORB-SLAM2的编译运行以及TUM数据集测试 徐大徐 2018.02.06 17:04 字数 1838 阅读 2167评论 0喜欢 2 近段时间一直在学习高翔博士的<视觉SLAM十四讲> ...