There are 3 types of edits that can be performed on strings:
Given 2 strings, write a function to check if they are 1 (or 0) edits away.
Example 1
pale, ple -> True
pales, pale -> True
pale, bale -> True
pale, bake -> True
Spellcheckers are great, right?!
Have you ever wondered how they know to recommend words when you’re making a typo? Of course there are much fancier algoriths and strategies than this, but think of this as a naive implementation!
Let’s try and formulate the 3 types of edits into rules we can check for.
1) String lengths difference is more than 1
2) Strings are identical length
3) String lengths differ by 1
We’ve mapped out all the cases, now let’s write them out.
Time complexity
O(min(n, m))
We’ll iterate over the shorter of the strings!
Space complexity
O(1)
No additional memory allocated.
Cracking the Coding Interview, CTCI, Chapter 1, Arrays and Strings, 1.5
Quick Links
Legal Stuff