Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Sep, 2022
Improve Article
Save Article

Given two strings str1 and str2, the task is to count all the valid strings. An example of a valid string is given below: 
If str1 = “toy” and str2 = “try”. Then S = “tory” is a valid string because when a single character is removed from it i.e. S = “tory” = “try” it becomes equal to str1. This property must also be valid with str2 i.e. S = “tory” = “toy” = str2. 
The task is to print the count of all possible valid strings.

Examples: 

Input: str = “toy”, str2 = “try” 
Output:
The given two words could be obtained from either word “tory” or word “troy”. So output is 2.

Input: str1 = “sweet”, str2 = “sheep” 
Output:
The two given word couldn’t be obtained from the same word by removing one letter. 

Approach: Calculate A as a longest common prefix of str1 and str2 and C as a longest common suffix of str1 and str2. If both the string are equal then 26 * (n + 1) strings are possible. Otherwise, set count = 0 and l equal to the first index in that is not a part of the common prefix and r is the rightmost index which is not a part of the common suffix.

Now, if str1[l+1 … r] = str2[l … r-1] then update count = count + 1
And if str1[l … r-1] = str2[l+1 … r] then update count = count + 1
Print the count in the end.

Below is the implementation of the approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of the
// required strings
int findAnswer(string str1, string str2, int n)
{
    int l, r;
    int ans = 2;
 
    // Searching index after longest common
    // prefix ends
    for (int i = 0; i < n; ++i) {
        if (str1[i] != str2[i]) {
            l = i;
            break;
        }
    }
 
    // Searching index before longest common
    // suffix ends
    for (int i = n - 1; i >= 0; i--) {
        if (str1[i] != str2[i]) {
            r = i;
            break;
        }
    }
 
    // If str1 = str2
    if (r < l)
        return 26 * (n + 1);
 
    // If only 1 character is different
    // in both the strings
    else if (l == r)
        return ans;
    else {
 
        // Checking remaining part of string
        // for equality
        for (int i = l + 1; i <= r; i++) {
            if (str1[i] != str2[i - 1]) {
                ans--;
                break;
            }
        }
 
        // Searching in right of string h
        // (g to h)
        for (int i = l + 1; i <= r; i++) {
            if (str1[i - 1] != str2[i]) {
                ans--;
                break;
            }
        }
 
        return ans;
    }
}
 
// Driver code
int main()
{
    string str1 = "toy", str2 = "try";
    int n = str1.length();
    cout << findAnswer(str1, str2, n);
    return 0;
}

Java




// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the count of the
// required strings
static int findAnswer(String str1, String str2, int n)
{
    int l = 0, r = 0;
    int ans = 2;
 
    // Searching index after longest common
    // prefix ends
    for (int i = 0; i < n; ++i)
    {
        if (str1.charAt(i) != str2.charAt(i))
        {
            l = i;
            break;
        }
    }
 
    // Searching index before longest common
    // suffix ends
    for (int i = n - 1; i >= 0; i--)
    {
        if (str1.charAt(i) != str2.charAt(i))
        {
            r = i;
            break;
        }
    }
 
    // If str1 = str2
    if (r < l)
        return 26 * (n + 1);
 
    // If only 1 character is different
    // in both the strings
    else if (l == r)
        return ans;
    else {
 
        // Checking remaining part of string
        // for equality
        for (int i = l + 1; i <= r; i++)
        {
            if (str1.charAt(i) != str2.charAt(i - 1))
            {
                ans--;
                break;
            }
        }
 
        // Searching in right of string h
        // (g to h)
        for (int i = l + 1; i <= r; i++)
        {
            if (str1.charAt(i-1) != str2.charAt(i))
            {
                ans--;
                break;
            }
        }
 
        return ans;
    }
}
 
// Driver code
public static void main(String args[])
{
    String str1 = "toy", str2 = "try";
    int n = str1.length();
    System.out.println(findAnswer(str1, str2, n));
     
}
}
 
// This code is contributed by
// Surendra_Gangwar

Python3




# Python3 implementation of the approach
import math as mt
 
# Function to return the count of
# the required strings
def findAnswer(str1, str2, n):
 
    l, r = 0, 0
    ans = 2
 
    # Searching index after longest
    # common prefix ends
    for i in range(n):
        if (str1[i] != str2[i]):
            l = i
            break
         
    # Searching index before longest
    # common suffix ends
    for i in range(n - 1, -1, -1):
        if (str1[i] != str2[i]):
            r = i
            break
         
    if (r < l):
        return 26 * (n + 1)
 
    # If only 1 character is different
    # in both the strings
    elif (l == r):
        return ans
    else:
 
        # Checking remaining part of
        # string for equality
        for i in range(l + 1, r + 1):
            if (str1[i] != str2[i - 1]):
                ans -= 1
                break
             
        # Searching in right of string h
        # (g to h)
        for i in range(l + 1, r + 1):
            if (str1[i - 1] != str2[i]):
                ans -= 1
                break
             
        return ans
     
# Driver code
str1 = "toy"
str2 = "try"
n = len(str1)
print(findAnswer(str1, str2, n))
 
# This code is contributed
# by Mohit kumar 29

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the count of the
// required strings
static int findAnswer(string str1, string str2, int n)
{
    int l = 0, r = 0;
    int ans = 2;
 
    // Searching index after longest common
    // prefix ends
    for (int i = 0; i < n; ++i)
    {
        if (str1[i] != str2[i])
        {
            l = i;
            break;
        }
    }
 
    // Searching index before longest common
    // suffix ends
    for (int i = n - 1; i >= 0; i--)
    {
        if (str1[i] != str2[i])
        {
            r = i;
            break;
        }
    }
 
    // If str1 = str2
    if (r < l)
        return 26 * (n + 1);
 
    // If only 1 character is different
    // in both the strings
    else if (l == r)
        return ans;
    else
    {
 
        // Checking remaining part of string
        // for equality
        for (int i = l + 1; i <= r; i++)
        {
            if (str1[i] != str2[i - 1])
            {
                ans--;
                break;
            }
        }
 
        // Searching in right of string h
        // (g to h)
        for (int i = l + 1; i <= r; i++)
        {
            if (str1[i-1] != str2[i])
            {
                ans--;
                break;
            }
        }
        return ans;
    }
}
 
// Driver code
public static void Main()
{
    String str1 = "toy", str2 = "try";
    int n = str1.Length;
    Console.WriteLine(findAnswer(str1, str2, n));
}
}
 
// This code is contributed by
// shs

PHP




<?php
// PHP implementation of the above approach
 
// Function to return the count of 
// the required strings
function findAnswer($str1, $str2, $n)
{
    $ans = 2;
 
    // Searching index after longest 
    // common prefix ends
    for ($i = 0; $i < $n; ++$i)
    {
        if ($str1[$i] != $str2[$i])
        {
            $l = $i;
            break;
        }
    }
 
    // Searching index before longest
    // common suffix ends
    for ($i = $n - 1; $i >= 0; $i--)
    {
        if ($str1[$i] != $str2[$i])
        {
            $r = $i;
            break;
        }
    }
 
    // If str1 = str2
    if ($r < $l)
        return 26 * ($n + 1);
 
    // If only 1 character is different
    // in both the strings
    else if ($l == $r)
        return $ans;
    else
    {
 
        // Checking remaining part of string
        // for equality
        for ($i = $l + 1; $i <= $r; $i++)
        {
            if ($str1[$i] != $str2[$i - 1])
            {
                $ans--;
                break;
            }
        }
 
        // Searching in right of string h
        // (g to h)
        for ($i = $l + 1; $i <= $r; $i++)
        {
            if ($str1[$i - 1] != $str2[$i])
            {
                $ans--;
                break;
            }
        }
 
        return $ans;
    }
}
 
// Driver code
$str1 = "toy";
$str2 = "try";
$n = strlen($str1);
 
echo findAnswer($str1, $str2, $n);
 
// This code is contributed by Ryuga
?>

Javascript




<script>
 
// Javascript implementation of the approach
 
    // Function to return the count of the
    // required strings
    function findAnswer( str1,  str2 , n)
    {
        var l = 0, r = 0;
        var ans = 2;
 
        // Searching index after longest common
        // prefix ends
        for (i = 0; i < n; ++i) {
            if (str1.charAt(i) != str2.charAt(i))
            {
                l = i;
                break;
            }
        }
 
        // Searching index before longest common
        // suffix ends
        for (i = n - 1; i >= 0; i--) {
            if (str1.charAt(i) != str2.charAt(i))
            {
                r = i;
                break;
            }
        }
 
        // If str1 = str2
        if (r < l)
            return 26 * (n + 1);
 
        // If only 1 character is different
        // in both the strings
        else if (l == r)
            return ans;
        else {
 
            // Checking remaining part of string
            // for equality
            for (i = l + 1; i <= r; i++) {
                if (str1.charAt(i) != str2.charAt(i - 1))
                {
                    ans--;
                    break;
                }
            }
 
            // Searching in right of string h
            // (g to h)
            for (i = l + 1; i <= r; i++) {
                if (str1.charAt(i - 1) != str2.charAt(i))
                {
                    ans--;
                    break;
                }
            }
 
            return ans;
        }
    }
 
    // Driver code
     
        var str1 = "toy", str2 = "try";
        var n = str1.length;
        document.write(findAnswer(str1, str2, n));
 
 
// This code contributed by gauravrajput1
 
</script>

Output

2

Complexity Analysis:

  • Time Complexity: O(N)
  • Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!