Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 1.02 KB

面试题01.05.一次编辑.md

File metadata and controls

46 lines (38 loc) · 1.02 KB

面试题 01.05.一次编辑

/*
 * @lc app=leetcode.cn id=面试题 01.05 lang=typescript
 *
 * [面试题 01.05] 一次编辑
 */

// @lc code=start
function oneEditAway(first: string, second: string): boolean {}
// @lc code=end

解法 1: 枚举

function oneEditAway(first: string, second: string): boolean {
  if (first.length > second.length) [first, second] = [second, first]

  let del = false
  if (second.length - first.length > 1) return false
  else if (second.length - first.length === 1) del = true

  let res = 0
  for (let i = 0, j = 0; j < second.length; j++, i++) {
    if (first[i] !== second[j]) {
      if (res) return false
      if (del) i--
      res = 1
    }
  }
  return true
}

Case

test.each([
  { input: { first: 'pale', second: 'ple' }, output: true },
  { input: { first: 'pales', second: 'pal' }, output: false },
])('input: first = $input.first, second = $input.second', ({ input: { first, second }, output }) => {
  expect(oneEditAway(first, second)).toEqual(output)
})