码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode01.05 一次编辑

时间:2020-06-23 00:39:24      阅读:40      评论:0      收藏:0      [点我收藏+]

标签:tco   img   for   相等   style   alt   span   second   编辑   

技术图片 

  只需要遍历一次,一旦遇到不相等的字符,比较剩余字符即可。

    public static boolean oneEditAway(String first, String second) {
        int firstLen=first.length();
        int secondLen=second.length();
        //保证 first 更长
        if(firstLen<secondLen){
            return oneEditAway(second,first);
        }
        //长度差大于 1 ,不可能只相差一个字符
        if(firstLen-secondLen>1){
            return false;
        }
        for(int i=0;i<secondLen;i++){
            //长度相同则比较剩余元素,长度不同尝试在 second 插入字符
            if(first.charAt(i)!=second.charAt(i)){
                return first.substring(i+1).equals(second.substring(firstLen==secondLen?i:i+1));
            }
        }
        return true;
    }

技术图片

 

Leetcode01.05 一次编辑

标签:tco   img   for   相等   style   alt   span   second   编辑   

原文地址:https://www.cnblogs.com/niuyourou/p/13179860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!