question one away there are three types of edits that can be performed on strings insert a character remove a character or replace a character given two strings write a function to check if they are one edit or zero edits away example pale ple true pales pale true pale bale true pale bake false source cracking the code interview 6th edition question 1 5 time complexity we are going through both strings at the same time and stopping when more than one letter is different which means on time complexity on the while loop no extra space is required size difference must be less than 1 compare strings while counting edits if letters differ update counter and compare next letter in this case if strings have different sizes increment only index of the longest otherwise increment both indexes only one edit is allowed if one string finished before the other we will certainly have one more edit to consider adding the last letter so we must check if the edit counter is still empty driver code str1 index str2 index size difference must be less than 1 compare strings while counting edits if letters differ update counter and compare next letter in this case if strings have different sizes increment only index of the longest otherwise increment both indexes only one edit is allowed if one string finished before the other we will certainly have one more edit to consider adding the last letter so we must check if the edit counter is still empty driver code
def is_one_away(str1, str2):
    edit_counter = 0
    i = 0 
    j = 0 

    
    if abs(len(str1) - len(str2)) > 1:
        return False

    
    
    
    
    while i < len(str1) and j < len(str2):
        if str1[i] != str2[j]:
            
            if edit_counter > 0:
                return False
            edit_counter += 1

            if len(str1) > len(str2):
                i += 1
                continue
            elif len(str1) < len(str2):
                j += 1
                continue
        i += 1
        j += 1

    
    
    
    if (i < len(str1) or j < len(str2)) and edit_counter > 0:
        return False

    return True


str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
print(is_one_away(str1, str2))