letcood刷题-一次编辑

文章介绍了一种判断两个字符串是否只需一次编辑操作(插入、删除或替换一个字符)就能相互转换的函数实现,提供了两种解法,一种通过比较字符串长度和切片操作,另一种是分别处理替换、插入和删除情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串有三种编辑操作:插入一个英文字符、删除一个英文字符或者替换一个英文字符。 给定两个字符串,编写一个函数判定它们是否只需要一次(或者零次)编辑。

示例 1:
输入:
first = "pale"
second = "ple"
输出: True

示例 2:
输入:
first = "pales"
second = "pal"
输出: False

解图如下:

var oneEditAway = function(first, second) {
    // 检查长度
    if(Math.abs(first.length - second.length) > 1) return false
    // 零编辑
    if (first === second) return true
    // 
    if (first.length > second.length) {
        [second, first] = [first, second]
    }
    for (let i = 0, len = first.length; i < len; i++) {
        if (first[i] !== second[i]) {
            return first.slice(i + 1) === second.slice(i + 1) // 字符串长度相等
                || first.slice(i) === second.slice(i + 1) // 字符串不等
        }
    }
    return true
};
// 解法2 ,这个比较蛮力,逻辑没有合并起来
var oneEditAway = function(first, second) {
   if(first.length == second.length) {// 替换
       return oneEditReplace(first,second)
   }else if(first.length+1 == second.length) { // 删除
       return oneEditInsert(first,second)
   }else if(first.length-1 == second.length) { // 插入
       return oneEditInsert(second,first)
   }
   return false
};
var oneEditReplace = function(first,second) {
   var findDif = false 
   for(var i=0;i<first.length;i++) {
       if(first.charAt(i)!=second.charAt(i)) {
          if(findDif) return false
          findDif = true
       } 
   }
   return true
};
var oneEditInsert = function(first,second) {
  var index1 = 0
  var index2 = 0
  while(index2 < second.length && index1 < first.length) {
      if(first.charAt(index1) != second.charAt(index2)) {
          if(index1 != index2) {
              return false
          }
      } else {
          index1++
      }
      index2++
  }
  return true
}
// 执行结果:通过显示详情
// 执行用时:72 ms, 在所有 TypeScript 提交中击败了78.95%的用户
// 内存消耗:43.8 MB, 在所有 TypeScript 提交中击败了76.32%的用户
// 通过测试用例:1146 / 1146

function oneEditAway(first: string, second: string): boolean {
    if(first.length > second.length + 1 || first.length < second.length - 1) return false
    if(first.length === second.length) {
        if(first === second) return true
        let cut = 0
        for(let i = 0; i < first.length; i++){
            if(first[i] !== second[i]) cut++
            if(cut > 1) return false
        }
        return true
    }else{
        const flag = first.length > second.length
        let long = flag ? first : second,
            short = flag ? second : first,
            temp = ''
        for(let i = 0; i < long.length; i++){
            if(long[i] !== short[i]){
                if(temp + long[i] + short.substring(i) === long || temp + long.substring(i + 1) === short) return true
                else return false
            }
            temp += long[i]
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值