Search
 
SCRIPT & CODE EXAMPLE
 

CPP

one away coding question

// C++ program to check if given two strings are
// at distance one.
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if edit distance between s1 and
// s2 is one, else false
bool isEditDistanceOne(string s1, string s2)
{
    // Find lengths of given strings
    int m = s1.length(), n = s2.length();
 
    // If difference between lengths is more than
    // 1, then strings can't be at one distance
    if (abs(m - n) > 1)
        return false;
 
    int count = 0; // Count of edits
 
    int i = 0, j = 0;
    while (i < m && j < n)
    {
        // If current characters don't match
        if (s1[i] != s2[j])
        {
            if (count == 1)
                return false;
 
            // If length of one string is
            // more, then only possible edit
            // is to remove a character
            if (m > n)
                i++;
            else if (m< n)
                j++;
            else //Iflengths of both strings is same
            {
                i++;
                j++;
            }
             
            // Increment count of edits
            count++;
        }
 
        else // If current characters match
        {
            i++;
            j++;
        }
    }
 
    // If last character is extra in any string
    if (i < m || j < n)
        count++;
 
    return count == 1;
}
 
// Driver program
int main()
{
   string s1 = "gfg";
   string s2 = "gf";
   isEditDistanceOne(s1, s2)?
           cout << "Yes": cout << "No";
   return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: fill vector with zeros c++ 
Cpp :: convert ascii char value to hexadecimal c++ 
Cpp :: loop c++ 
Cpp :: clear previous terminal output c++ 
Cpp :: oncomponentendoverlap ue4 c++ 
Cpp :: onoverlapbegin ue4 c++ 
Cpp :: resharper fold statement 
Cpp :: c++ get active thread count 
Cpp :: c++ if statement 
Cpp :: cpp vscode multipe compilation 
Cpp :: converting char to integer c++ 
Cpp :: size of string c++ 
Cpp :: volumeof a sphere 
Cpp :: how to make randomizer c++ 
Cpp :: closing a ifstream file c++ 
Cpp :: integer max value c++ 
Cpp :: async multi thread 
Cpp :: remove element from c++ 
Cpp :: C++, binary search recursive 
Cpp :: c++ function pointer variable 
Cpp :: ex: cpp 
Cpp :: cpp language explained 
Cpp :: convert uppercase to lowercase 
Cpp :: conversion of class type data into basic type data in c++ 
Cpp :: qt c++ qdockwidget remove title 
Cpp :: how to get characters through their ascii value in c++ 
Cpp :: how to read rotary encoder c++ 
Cpp :: c create 1 bit value 
Cpp :: turbo c++ easy programs 
Cpp :: pallindrome string 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =