One edit distance

problem [https://leetcode.com/problems/one-edit-distance/ 161. One Edit Distance].

Problem description
Given two strings  and , return   if they are both one edit distance apart, otherwise return.

A string  is said to be one distance apart from a string   if you can:
 * Insert exactly one character into  to get.
 * Delete exactly one character from  to get.
 * Replace exactly one character of  with a different character to get.


 * Example 1
 * Input:
 * Output: true
 * Explanation: We can insert 'c' into s to get t.


 * Example 2
 * Input:
 * Output: false
 * Explanation: We cannot get t from s by only one step.


 * Example 3
 * Input:
 * Output: true


 * Example 4
 * Input:
 * Output: true


 * Constraints
 * and  consist of lower-case letters, upper-case letters and/or digits.
 * and  consist of lower-case letters, upper-case letters and/or digits.
 * and  consist of lower-case letters, upper-case letters and/or digits.

Analysis
While one may be tempted to solve the Edit distance problem first, it is actually not needed to do so.

We notice several facts that can lessen our burden of computation:
 * If the two strings are the same, then return false.
 * If the two strings' lengths differ by 2 or greater, then return false.
 * Two strings that are one edit distance apart either have the same length (i.e. one character is replaced) or have length difference of 1 (i.e. one character is inserted / deleted).

So we can do the following:
 * 1) Check if the two strings are equal. If equal, then return false.
 * 2) Check if the two strings' lengths differ by 2 or greater. If yes, then return false.
 * 3) Check if  . If yes, we we swap the order of the two strings and recursively call the function.
 * 4) Consider the following cases:
 * 5) *# Set.
 * 6) *# Traverse both strings from left to right. Compare them character by character.
 * 7) *#* If :
 * 8) *#** If  then return false.
 * 9) *#** Otherwise set . Progress to the next position.
 * 10) *#* Otherwise progress to the next position.
 * 11) *# If we have traversed to the end, then return true.
 * 12) *# Set.
 * 13) *# Traverse both strings from left to right. Compare them character by character.
 * 14) *#* If :
 * 15) *#** If, then return false.
 * 16) *#** Otherwise progress to next position.
 * 17) *#* Otherwise:
 * 18) *#** If, then set  . Keep the same   and move on to compare   vs..
 * 19) *#** Otherwise move on to the next.
 * 20) *# If we have traversed to the end, then return true.
 * 1) *#** Otherwise move on to the next.
 * 2) *# If we have traversed to the end, then return true.

Time complexity

 * 1) Comparing whether two strings are the same takes O(n) time, where n is the length of the shorter input string.
 * 2) Since we traverse the two strings character by character, the time complexity for this is O(n), where n is again the length of the shorter input string.

So the total time complexity is O(n).

Space complexity

 * Stack
 * Since we only have at most one level of recursion, and other auxiliary variables take constant space, the space complexity on stack is O(1).


 * Heap
 * Since we did not allocate any new objects, the space complexity on heap is O(1).

So the total space complexity is O(1).