[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 ...
随机推荐
- hdoj 1004 学习思路
hdoj 1004题目大概讲的是,将输入的字符串根据输入次数多少,输出出现次数最多的字符串. 题目逻辑很简单,就是需要选择相应的数据结构,看了别人提交的discuss,明显发现可以使用多种数据结构解这 ...
- ThinkPHP子类继承Controller类的注意事项
在实际的开发中,往往有很多子类都继承自同一个父类,然后该父类再继承自框架内置类的需求. 比如: class Init extends Controller{...} class son1 extend ...
- 关于PHP Notice: A non well formed numeric value encountered, 你知道多少
---------------------------------------------------------------------------------------------- A non ...
- Python unindent dese not match any out indentation level 问题
今天写个小程序出现 “unindent dese not match any out indentation level”. 一直没找到原因,经过仔细对比发现实际上是缩进的问题. 上下两行的缩进用的 ...
- 吴裕雄 python 爬虫(2)
import requests from bs4 import BeautifulSoup url = 'http://www.baidu.com' html = requests.get(url) ...
- 处女座与cf-模拟
链接:https://ac.nowcoder.com/acm/contest/327/B来源:牛客网 题目描述 众所周知,处女座经常通过打cf来调节自己的心情.今天处女座又参加了一场cf的比赛,他知道 ...
- 深入理解hello world
阅读目录 为什么所有东西都是从类开始的 为什么总是需要有一个“main”方法 HelloWorld的字节码 HelloWorld在JVM中是如何运行的 对于每个Java程序员来说,HelloWorld ...
- mysql ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log. 一般可通 ...
- CentOS7下解决yum install mysql-server没有可用包
# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm -- http://repo.mysql.com/mysq ...
- Light Probe Proxy Volume
[Light Probe Proxy Volume] The Light Probe Proxy Volume (LPPV) component allows you to use more ligh ...