1-5-one-edit-away Algorithm

The 1-5-one-edit-away Algorithm is a powerful technique designed to improve the quality and consistency of content, particularly in the realm of content creation, editing, and proofreading. This innovative algorithm follows a simple yet effective rule: that any piece of text, regardless of its current state, is just one edit away from being perfect. By breaking down the editing process into small, manageable steps, the 1-5-one-edit-away Algorithm aims to make content editing more efficient, precise, and targeted, ultimately resulting in a higher quality output. The basic premise of this algorithm is that the editor looks at the content in segments, focusing on one segment at a time and making one crucial edit at a time. This approach encourages the editor to pay close attention to each word, sentence, and paragraph, identifying errors and inconsistencies while also providing an opportunity to enhance the overall flow and readability of the content. The 1-5-one-edit-away Algorithm not only streamlines the editing process, but it also fosters a more thorough and meticulous approach to content creation, ensuring that the final product is polished, engaging, and error-free.
/* 
 * Problem: There are three possible edits that can be performed on a string.
 * 1. Insert a char.
 * 2. Delete a char.
 * 3. Replace a char.
 *
 * Given two strings, determine if they are one or 0 edit away.
 *
 * Approach :
 * 1. Case when strings are of some length --> possible edit is replace.
 *    If there are more than one mismatch, return false
 *
 * 2. Case when One string is bigger than another
 *    Smaller string ------------> Bigger String
 *                     insert
 *                     delete
 *    smaller string <-----------  Bigger String
 *
 *    Idea is check if there are more than one mismatch discounting the already
 *    difference in the string. Therefore for first mismatch we do not move the pointer
 *    pointing to smaller string, and then expect it to match from next char of bigger
 *    string.
 */



#include <iostream>
#include <string>
#include <cmath>


bool oneEditAway( const std::string & str1, const std::string & str2 )
{
    if ( std::abs( int(str1.length()) - int(str2.length()))  > 1 ) {
        return false;
    }

    int len1 = str1.length();
    int len2 = str2.length();
    std::string smaller = len1 < len2 ? str1 : str2;
    std::string bigger =  len1 < len2 ? str2 : str1;
    
    unsigned int i = 0, j = 0; 
    bool mismatchDone = false;
    while ( i < smaller.length() && j < bigger.length() )
    {
        if ( smaller[i] != bigger[j] ) {
            if (mismatchDone) {
                return false;
            }
            mismatchDone = true;
            if ( len1 == len2 ) {
                ++i;   //case of replace
            }
        } else {
                ++i;   //move short pointer if its a match, dont move it in case of first mismatch
        }
        ++j;           //always move long string pointer.
    }
    return true;
}


void translate( bool result, const std::string str1, const std::string str2 )
{
    if (result == true ) {
        std::cout << str1 << " and " << str2 << " are one edit away\n";
    } else {
        std::cout << str1 << " and " << str2 << " are not one edit away\n";
    }
}

int main()
{
    translate ( oneEditAway("pale", "ple"), "pale", "ple" );
    translate ( oneEditAway("pales", "pale"), "pales", "pale" );
    translate ( oneEditAway("pale", "pales"), "pale", "pales" );
    translate ( oneEditAway("pale", "bale"), "pale", "bale" );
    translate ( oneEditAway("pale", "bake"), "pale", "bake" );
    return 0;

}

LANGUAGE:

DARK MODE: