📜  一次删除后变成等于两个字符串之一的字符串的计数

📅  最后修改于: 2021-05-20 05:46:38             🧑  作者: Mango

给定两个字符串str1str2 ,任务是计算所有有效字符串。有效字符串的示例如下:
如果str1 =“玩具”str2 =“尝试” 。则S =“托里”是一个有效的字符串,因为当一个字符从它除去即S =“吨ÕRY” =“尝试”,它变成等于STR1。此属性还必须在str2中有效,即S =“ to r y” =“ toy” = str2。
该任务是打印所有可能的有效字符串的计数。
例子:

方法:计算作为str1STR2赛车C的最长公共前缀为str1STR2的最长公共后缀。如果两个字符串相等,则可以使用26 *(n + 1)个字符串。否则,将count设置为0l等于第一个索引,因为它不是公共前缀的一部分,而r是最右端的索引,这不是公共后缀的一部分。
现在,如果str1 [l + 1…r] = str2 [l…r-1],则更新count = count +1
如果str1 [l…r-1] = str2 [l + 1…r],则更新count = count + 1
最后打印计数
下面是该方法的实现:

C++
// C++ implementation of the approach
#include 
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
= 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


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。