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:

  1. Insert a character into s to get t
  2. Delete a character from s to get t
  3. 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的更多相关文章

  1. [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 ...

  2. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  3. 161. One Edit Distance

    题目: Given two strings S and T, determine if they are both one edit distance apart. 链接: http://leetco ...

  4. [LeetCode#161] One Edit Distance

    Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...

  5. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

  6. [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 ...

  7. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  8. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

随机推荐

  1. c++ 深度优先算法

    #include <iostream> using namespace std; #define VertexNum 9 /*定义顶点数*/ struct Node /*声明图形顶点结构* ...

  2. Jackknife,Bootstrap, Bagging, Boosting, AdaBoost, RandomForest 和 Gradient Boosting的区别

    Bootstraping: 名字来自成语“pull up by your own bootstraps”,意思是依靠你自己的资源,称为自助法,它是一种有放回的抽样方法,它是非参数统计中一种重要的估计统 ...

  3. centos7.4 测试CPU压力--命令搞定

    直接输入命令CPU消耗增加: cat /dev/urandom | gzip - > /dev/null 停止: 直接Ctrl+c结束

  4. tensorflow 损失计算--MSN

    1.tf.losses.mean_squared_error函数 tf.losses.mean_squared_error( labels, predictions, weights=1.0, sco ...

  5. split和join合写

    小骆驼 第一章 简介 1. // input 和截取字符 #!usr/bin/perl use strict; use warnings; while ( <> ) { chomp; pr ...

  6. Dynamics CRM - 为 Form 或者字段设置 Error Notification

    在 Dynamics CRM 开发中,我们一般要利用 JS 来做一些数据验证的功能,我们也需要将验证结果显示出来,比起直接 alert 出信息来提示用户的方式,CRM 提供了更加美观和人性化的方式来通 ...

  7. 34. docker swarm Dockerstack 部署 wordpress

    1. 查看 docker compose    depoly 语法 官网地址 : https://docs.docker.com/compose/compose-file/#deploy ENDPOI ...

  8. h5-其他伪元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. mybatis-关于<update>的日常记录

    !注意:,一定要有 <update id="updateByPrimaryKeySelective" parameterType="com.dhht.model.o ...

  10. 吴裕雄--天生自然 JAVA开发学习:StringBuffer、数组

    public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer(& ...