Fast typists often swap two neighboring letters, like teh for the. A search box wants a typo distance that counts such a swap as a single edit. The allowed edits are: insert a character, delete a character, replace a character, or swap two neighboring characters.
There is one restriction: once two characters have been swapped, neither of them may be edited again, and nothing may be inserted between them. Write typoDistance(a, b) that returns the fewest edits that turn a into b under these rules.
Input: a = "teh", b = "the" Output: 1
Swap e and h.
Input: a = "recieve", b = "receive" Output: 1
Swap i and e. Plain edit distance would count 2 replacements.
0 <= a.length, b.length <= 500Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #540 Typo Distance With Swaps. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.