Question

In: Computer Science

C# One Away: There are three types of edits that can be performed on strings: insert...

C#

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, pIe -> true
pales. pale -> true
pale. bale -> true
pale. bake -> false

Comment your code to explain it.

Solutions

Expert Solution

C# PROGRAM ===>

using System;

namespace ConsoleApp
{

   class OneEditAway
   {
       static bool isOneEdit(String s1,String s2)
       {

           // lengths of strings s1 ,s2
           int m = s1.Length, n = s2.Length;

           // if difference is more than one return false
           if (Math.Abs(m - n) > 1)
{
               return false;
           }
              

           int count = 0;
           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++;
                   }
                   // If lengths of both strings is same
                   else
                   {
                       i++;
                       j++;
                   }

                   // Increment count of edits
                   count++;
               }
               // If current characters match
               else
               {
                   i++;
                   j++;
               }
           }

           // If last character is extra in any string
           if (i < m || j < n)
{
               count++;
           }
              

           return count == 1;
       }

         
       public static void Main()
       {
           Console.Write("Enter String 1 : ");
           String str1 = Console.ReadLine();
           Console.Write("Enter String 2 : ");
           String str2 = Console.ReadLine();
           Console.WriteLine();

           if (isOneEdit(str1, str2))
               Console.WriteLine(str1+ " , "+str2+ " --> "+"true");
           else
               Console.WriteLine(str1 + " , " + str2 + " --> " + "false");
       }
   }
}  

OUTPUT SCREENSHOT ===>


Related Solutions

Describe the types of arrays and operations that can be performed of them. [10
Describe the types of arrays and operations that can be performed of them. [10
Describe the three types of problems that can be solved on computers and provide one example...
Describe the three types of problems that can be solved on computers and provide one example for each problem.   
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
One can encrypt numbers as strings of letters by applying a substitution cipher. For example, the...
One can encrypt numbers as strings of letters by applying a substitution cipher. For example, the substitution 0 <-> F, 1 <-> G, 2 <-> D, 3 <-> Z, would encrypt the number 30231 as the string of letters ZFDZG. Decipher the following encrypted equation involving three numbers: AB + BC + ACA = BCB. (You may need to use the fact that our usual numbers are represented in base 10, for example, the number 838 is represented with digits...
There are three different types of tests we learned. What one of the three types is...
There are three different types of tests we learned. What one of the three types is the following? “ A researcher estimates that high school girls miss more days of school than high school boys. A sample of 16 girls showed that they missed an average of 3.9 days of school and a sample of 22 boys showed that they missed an average of 3.6 days. The standard deviation of the 16 girls was .6 and the standard deviation of...
In 250 words or more: What types of warrantLESS searches can be performed by law enforcement...
In 250 words or more: What types of warrantLESS searches can be performed by law enforcement collecting digital evidence? Give one example for each and the reasons why are warrants not required?
Explain the three types of tectonic plate boundaries, and the three sub-types of the one boundary....
Explain the three types of tectonic plate boundaries, and the three sub-types of the one boundary. In your explanation you must include: The type of boundary What is happening at this boundary The type of features/events that are common with this boundary An example of where this type of boundary exists (on this one, make sure you explain what has happened at this specific location) in geography
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT