zoukankan      html  css  js  c++  java
  • leetcode161

    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.

    分类讨论,先删除简单易判断的case,柿子挑软的捏。
    1.长度相差太大(>1)必定false
    2.长度相等,要求有一个ch不同。(发现第一个不同后对比后面剩下的)
    3.长度差一,要求有一个ch插入。(发现第一个不同后要求短的现在开始剩下的==长的后面剩下的)

    可以合并2,3,通过用 || ,多对比几个其中有一个成立即可啊。

    我的实现:

    class Solution {
        public boolean isOneEditDistance(String s, String t) {
            
            int lenDiff = Math.abs(s.length() - t.length());
            if( lenDiff > 1) {
                return false;
            } else if (lenDiff == 0) {
                for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) != t.charAt(i)) {
                        return i == s.length() - 1 
                            || s.substring(i + 1, s.length()).equals(t.substring(i + 1, t.length()));
                    }
                }
                return false;
            } else {
                if (s.length() > t.length()) {
                    String temp = s;
                    s = t;
                    t = temp;
                }
                for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) != t.charAt(i)) {
                        return s.substring(i, s.length()).equals(t.substring(i + 1, t.length()));
                    }
                }
                return true;
            }
        }
    }

    九章实现:

    /**
    * 本参考程序来自九章算法,由 @lin 提供。版权所有,转发请注明出处。
    * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
    * - 现有的面试培训课程包括:九章算法班,系统设计班,算法强化班,Java入门与基础算法班,Android 项目实战班,
    * - Big Data 项目实战班,算法面试高频题班, 动态规划专题班
    * - 更多详情请见官方网站:http://www.jiuzhang.com/?source=code
    */ 
    
    public class Solution {
        /**
         * @param s: a string
         * @param t: a string
         * @return: true if they are both one edit distance apart or false
         */
        public boolean isOneEditDistance(String s, String t) {
            // write your code here
            if(s == null || t == null){
                return s == t;
            }
            if(s.equals(t)){
                return false;
            }
            
            int n = s.length();
            int m = t.length();
            if(Math.abs(n-m)>1){
                return false;
            }
            int index = 0; 
            int len = Math.min(m, n);
            for(int i = 0; i < len; i++){
                index = i + 1;
                if(s.charAt(i) != t.charAt(i)){
                    return s.substring(index).equals(t.substring(index)) 
                    || s.substring(index).equals(t.substring(index-1))
                    || s.substring(index-1).equals(t.substring(index));
                }
            }
            
            return true;
        }
    }
  • 相关阅读:
    JavaScript-循环
    JavaScript-条件判断
    JavaScript-对象
    Vue快速入门
    Typora中的MarkDown语法
    (已解决)ERROR: In file './docker-compose.yml', service 'networks' must be a mapping not an array
    mac常用快捷键
    Python数据分析
    Python列表和元组
    Selenium工具爬取商品
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9613459.html
Copyright © 2011-2022 走看看