Count of strings that become equal to one of the two strings after one removal in C++


We are given with two different strings let's say s1 and s2 and the task is to form the string let's say S by combining the unique letters of s1 and s2 and now check whether after removing one character from the string S it is forming a string which will be equal to string s1 OR s2.

For Example

Input - string S1 = "utter", string S2 = "butter"; 

Output - Count of strings that become equal to one of the two strings after one removal are: 1

Explanation -  we are given with string s1 and s2 and we will from string S i.e. 'butter' and now if we remove letter 'b' from the string S it will become 'utter' which is equal to the string s1 therefore the count is 1.

Input - string S1 = "fat", string S2 = "rat"; 

Output - Count of strings that become equal to one of the two strings after one removal are: 2

Explanation - we are given with string s1 and s2 and we will form string S i.e. 'frat' and now if we remove letter 'rs' from the string S it will become 'fat' which is equal to the string s1 and if we remove the letter 'f' it will become 'rat' which is equal to the string s2 therefore the count is 2.

Approach used in the below program is as follows

  • Declare two strings s1 and s2 and calculate the size of string s1 and pass the data to the function for further processing.
  • Declare a variable count and set it as 2 because the maximum output which can be possible is 2.
  • Declare two temporary variables as start and end for the loop traversal.
  • Start loop FOR from 0 till the size of a string s1 and inside the loop check IF s1[i] not equals to s2 then set start as i and break
  • Start another loop FOR from i till one less size of string s1 and inside the loop check IF s1 not equals to s2 then set end as i and break
  • Check IF value of end is less then start then set count as 26 * (size of string s1 + 1) and return count.
  • ELSE IF check start equals end then return count
  • ELSE, start loop FOR from i to start +1 till the end and inside the loop check IF s1[i] not equals to s2[i-1] then decrement the count by 1 and break
  • Start loop FOR from i to start + 1 till end and check IF s1[i-1] not equals to s2[i] then decrement the count by 1 and break
  • Return the count.
  • Print the result.

Example

Live Demo

#include <bits/stdc++.h>
using namespace std;

int equal_removal(string S1, string S2, int size_S1) {
   int count = 2;
   int start;
   int end;

   for (int i = 0; i < size_S1; ++i) {
      if (S1[i] != S2[i]) {
         start = i;
         break;
      }
   }
   for (int i = size_S1 - 1; i >= 0; i--) {
      if (S1[i] != S2[i]) {
         end = i;
         break;
      }
   }
   if (end < start) {
      count = 26 * (size_S1 + 1);
      return count;
   } else if (start == end) {
      return count;
   } else {
      for (int i = start + 1; i <= end; i++) {
         if (S1[i] != S2[i - 1]) {
            count--;
            break;
         }
      }
      for (int i = start + 1; i <= end; i++) {
         if (S1[i - 1] != S2[i]) {
            count--;
            break;
         }
      }
      return count;
   }
}
int main() {
   string S1 = "utter";
   string S2 = "butter";
   int size_S1 = S1.length();
   cout << "Count of strings that become equal to one of the two strings after one removal are: " << equal_removal(S1, S2, size_S1);
   return 0;
}

If we run the above code it will generate the following output −

Output

Count of strings that become equal to one of the two strings after one removal are: 1

Updated on: 29-Jan-2021

408 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements