|
| 1 | +''' |
| 2 | + You're given two strings stringone and stringtwo. Write a function that determines if these |
| 3 | + two strings can be made equal using only one edit. |
| 4 | +
|
| 5 | +
|
| 6 | + There are 3 possible edits: |
| 7 | + Replace: One character in one string is swapped for a different character. |
| 8 | + Add:: One character is added at any index in one string. |
| 9 | + Remove: One character is removed at any index in one string. |
| 10 | +
|
| 11 | + Sample Input: StringOne: alaska StringTwo: aloska |
| 12 | + Output: True |
| 13 | +
|
| 14 | +
|
| 15 | + Explanation: |
| 16 | + The code snippet is implementing the "One Edit Away" algorithm, which determines whether two given |
| 17 | + strings are one edit away from each other. An edit is defined as either inserting a character, removing a character, or replacing a character. |
| 18 | +
|
| 19 | + The `OneEdit` function takes two strings as input and returns a boolean indicating whether |
| 20 | + they are one edit away. Here's the breakdown of the algorithm: |
| 21 | +
|
| 22 | + 1. Calculate the lengths of the two strings. |
| 23 | + 2. Check if the difference in lengths is greater than 1. If so, return `false` because it's |
| 24 | + not possible to make one edit to make the strings equal. |
| 25 | + 3. Traverse both strings until the shorter one is fully traversed or an unequal character is |
| 26 | + found. |
| 27 | + 4. If an unequal character is found, check the remaining portion of the strings to determine |
| 28 | + if they are still one edit away. |
| 29 | + 5. Check the remaining characters in the longer string compared to the remaining characters |
| 30 | + in the shorter string. |
| 31 | + 6. Return `true` if the remaining portions match, indicating they are one edit away. |
| 32 | + 7. If the loop completes without finding any unequal characters, the strings are either |
| 33 | + identical or differ only in length by 1, which means they are one edit away. |
| 34 | + 8. The `abs` and `min` functions are utility functions used to calculate the absolute value |
| 35 | + and minimum of two integers, respectively. |
| 36 | +
|
| 37 | + The algorithm efficiently checks for the possibility of one edit by comparing the characters at corresponding indices and handling cases where the lengths of the strings are different. |
| 38 | +
|
| 39 | + O(n) time | O(1) space - where n is the length of the shorter string |
| 40 | +
|
| 41 | +''' |
| 42 | +def OneEdit(stringOne, stringTwo): |
| 43 | + lengthOne = len(stringOne) |
| 44 | + lengthTwo = len(stringTwo) |
| 45 | + |
| 46 | + # Check the difference in lengths between the two strings. |
| 47 | + # If the difference is greater than 1, it is not possible to make one edit to make them equal. |
| 48 | + if abs(lengthOne - lengthTwo) > 1: |
| 49 | + return False |
| 50 | + |
| 51 | + # Traverse the strings until the shorter one is fully traversed or an unequal character is found. |
| 52 | + for i in range(min(lengthOne, lengthTwo)): |
| 53 | + # If an unequal character is found, check the remaining portion of the strings to determine if they are still one edit away. |
| 54 | + if stringOne[i] != stringTwo[i]: |
| 55 | + # Check the remaining characters in the longer string compared to the remaining characters in the shorter string. |
| 56 | + # Return True if they match, indicating they are one edit away. |
| 57 | + if lengthOne > lengthTwo: |
| 58 | + return stringOne[i+1:] == stringTwo[i:] |
| 59 | + elif lengthTwo > lengthOne: |
| 60 | + return stringTwo[i+1:] == stringOne[i:] |
| 61 | + else: |
| 62 | + return stringOne[i+1:] == stringTwo[i+1:] |
| 63 | + |
| 64 | + # If the loop completes without finding any unequal characters, the strings are either identical or differ only in length by 1. |
| 65 | + return True |
| 66 | + |
| 67 | +def abs(a): |
| 68 | + if a < 0: |
| 69 | + return -a |
| 70 | + return a |
| 71 | + |
| 72 | +def min(a, b): |
| 73 | + if a < b: |
| 74 | + return a |
| 75 | + return b |
0 commit comments