Count of strings that become equal to one of the two strings after one removal
Last Updated :
01 Nov, 2023
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: 2
The given two words could be obtained from either word “tory” or word “troy”. So output is 2.
Input: str1 = “sweet”, str2 = “sheep”
Output: 0
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++
#include <bits/stdc++.h>
using namespace std;
int findAnswer(string str1, string str2, int n)
{
int l, r;
int ans = 2;
for ( int i = 0; i < n; ++i) {
if (str1[i] != str2[i]) {
l = i;
break ;
}
}
for ( int i = n - 1; i >= 0; i--) {
if (str1[i] != str2[i]) {
r = i;
break ;
}
}
if (r < l)
return 26 * (n + 1);
else if (l == r)
return ans;
else {
for ( int i = l + 1; i <= r; i++) {
if (str1[i] != str2[i - 1]) {
ans--;
break ;
}
}
for ( int i = l + 1; i <= r; i++) {
if (str1[i - 1] != str2[i]) {
ans--;
break ;
}
}
return ans;
}
}
int main()
{
string str1 = "toy" , str2 = "try" ;
int n = str1.length();
cout << findAnswer(str1, str2, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findAnswer(String str1, String str2, int n)
{
int l = 0 , r = 0 ;
int ans = 2 ;
for ( int i = 0 ; i < n; ++i)
{
if (str1.charAt(i) != str2.charAt(i))
{
l = i;
break ;
}
}
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (str1.charAt(i) != str2.charAt(i))
{
r = i;
break ;
}
}
if (r < l)
return 26 * (n + 1 );
else if (l == r)
return ans;
else {
for ( int i = l + 1 ; i <= r; i++)
{
if (str1.charAt(i) != str2.charAt(i - 1 ))
{
ans--;
break ;
}
}
for ( int i = l + 1 ; i <= r; i++)
{
if (str1.charAt(i- 1 ) != str2.charAt(i))
{
ans--;
break ;
}
}
return ans;
}
}
public static void main(String args[])
{
String str1 = "toy" , str2 = "try" ;
int n = str1.length();
System.out.println(findAnswer(str1, str2, n));
}
}
|
Python3
import math as mt
def findAnswer(str1, str2, n):
l, r = 0 , 0
ans = 2
for i in range (n):
if (str1[i] ! = str2[i]):
l = i
break
for i in range (n - 1 , - 1 , - 1 ):
if (str1[i] ! = str2[i]):
r = i
break
if (r < l):
return 26 * (n + 1 )
elif (l = = r):
return ans
else :
for i in range (l + 1 , r + 1 ):
if (str1[i] ! = str2[i - 1 ]):
ans - = 1
break
for i in range (l + 1 , r + 1 ):
if (str1[i - 1 ] ! = str2[i]):
ans - = 1
break
return ans
str1 = "toy"
str2 = "try"
n = len (str1)
print (findAnswer(str1, str2, n))
|
C#
using System;
class GFG
{
static int findAnswer( string str1, string str2, int n)
{
int l = 0, r = 0;
int ans = 2;
for ( int i = 0; i < n; ++i)
{
if (str1[i] != str2[i])
{
l = i;
break ;
}
}
for ( int i = n - 1; i >= 0; i--)
{
if (str1[i] != str2[i])
{
r = i;
break ;
}
}
if (r < l)
return 26 * (n + 1);
else if (l == r)
return ans;
else
{
for ( int i = l + 1; i <= r; i++)
{
if (str1[i] != str2[i - 1])
{
ans--;
break ;
}
}
for ( int i = l + 1; i <= r; i++)
{
if (str1[i-1] != str2[i])
{
ans--;
break ;
}
}
return ans;
}
}
public static void Main()
{
String str1 = "toy" , str2 = "try" ;
int n = str1.Length;
Console.WriteLine(findAnswer(str1, str2, n));
}
}
|
Javascript
<script>
function findAnswer( str1, str2 , n)
{
var l = 0, r = 0;
var ans = 2;
for (i = 0; i < n; ++i) {
if (str1.charAt(i) != str2.charAt(i))
{
l = i;
break ;
}
}
for (i = n - 1; i >= 0; i--) {
if (str1.charAt(i) != str2.charAt(i))
{
r = i;
break ;
}
}
if (r < l)
return 26 * (n + 1);
else if (l == r)
return ans;
else {
for (i = l + 1; i <= r; i++) {
if (str1.charAt(i) != str2.charAt(i - 1))
{
ans--;
break ;
}
}
for (i = l + 1; i <= r; i++) {
if (str1.charAt(i - 1) != str2.charAt(i))
{
ans--;
break ;
}
}
return ans;
}
}
var str1 = "toy" , str2 = "try" ;
var n = str1.length;
document.write(findAnswer(str1, str2, n));
</script>
|
PHP
<?php
function findAnswer( $str1 , $str2 , $n )
{
$ans = 2;
for ( $i = 0; $i < $n ; ++ $i )
{
if ( $str1 [ $i ] != $str2 [ $i ])
{
$l = $i ;
break ;
}
}
for ( $i = $n - 1; $i >= 0; $i --)
{
if ( $str1 [ $i ] != $str2 [ $i ])
{
$r = $i ;
break ;
}
}
if ( $r < $l )
return 26 * ( $n + 1);
else if ( $l == $r )
return $ans ;
else
{
for ( $i = $l + 1; $i <= $r ; $i ++)
{
if ( $str1 [ $i ] != $str2 [ $i - 1])
{
$ans --;
break ;
}
}
for ( $i = $l + 1; $i <= $r ; $i ++)
{
if ( $str1 [ $i - 1] != $str2 [ $i ])
{
$ans --;
break ;
}
}
return $ans ;
}
}
$str1 = "toy" ;
$str2 = "try" ;
$n = strlen ( $str1 );
echo findAnswer( $str1 , $str2 , $n );
?>
|
Complexity Analysis:
- Time Complexity: O(N), The function findAnswer() consists of several loops with linear time complexity, and some basic conditional statements with constant time complexity. Therefore, the overall time complexity of the function is O(n), where n is the length of the input strings.
- Auxiliary Space: O(1), The function findAnswer() uses a few integer variables to store the left and right indices of the non-matching characters, as well as an integer variable to store the answer. It also uses two string variables to store the input strings. Therefore, the overall space complexity of the function is O(1), which is constant space complexity.