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
For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G,...
For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G, T}. Implement a special hash table to store all short DNA segments of certain length (e.g., 20) found in a long DNA sequence. Every DNA segment is stored as a (key, value) pair, where the key is the sequence of the segment, and the value is the position of the segment in the DNA. For example given a DNA sequence of ACGTACGTTT, there are...
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...
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...
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...
In language C Have the program present a menu where I can either: Insert a new...
In language C Have the program present a menu where I can either: Insert a new integer onto the stack. Process an integer from the stack. Quit the program. Every time that you present the menu, please print out the contents of the stack before I pick a menu option. If the stack is empty, please let the user know. The point of this assignment is to use dynamic memory allocation. So you must use malloc at the start of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT