/*
* @lc app=leetcode.cn id=面试题 01.05 lang=typescript
*
* [面试题 01.05] 一次编辑
*/
// @lc code=start
function oneEditAway(first: string, second: string): boolean {}
// @lc code=end
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
}
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)
})