📌  相关文章
📜  检查S中以字符串S1为前缀、以S2为后缀的子串个数是否等于以S2为前缀、以S1为后缀的子串个数

📅  最后修改于: 2021-09-07 02:23:36             🧑  作者: Mango

给定三个字符串SS1S2 ,任务是检查以S1S2开头和结尾的子字符串的数量是否等于以S2S1开头和结尾的子字符串的数量。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

处理方法:按照以下步骤解决问题:

  • 将字符串SS1S2的长度分别存储在变量中,比如NN1N2
  • 初始化一个变量,比如count ,以存储子字符串S1在字符串S中出现的次数。
  • 初始化一个变量,比如ans ,以存储分别以S1S2开头和结尾的子字符串。
  • 遍历给定的字符串S并执行以下步骤:
    • 将字符串S的子字符串从大小为N1的索引i开始存储在字符串prefix 中
    • 将字符串S的子字符串存储在字符串suffix 中,从大小为N2的索引i开始。
    • 如果前缀与字符串S1相同,则将count的值增加1
    • 如果后缀与字符串S2相同,则将ans的值增加count
  • 使用上述步骤,找出分别以S2S1开头和结尾的子串的数量。将获得的计数存储在变量 say ans2 中
  • 如果发现ansans2的值相等,则打印“Yes” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count number of
// substrings that starts with
// string S1 and ends with string S2
int countSubstrings(string S, string S1,
                    string S2)
{
    // Stores the length of each string
    int N = S.length();
    int N1 = S1.length();
    int N2 = S2.length();
 
    // Stores the count of prefixes
    // as S1 and suffixes as S2
    int count = 0, totalcount = 0;
 
    // Traverse string S
    for (int i = 0; i < N; i++) {
 
        // Find the prefix at index i
        string prefix = S.substr(i, N1);
 
        // Find the suffix at index i
        string suffix = S.substr(i, N2);
 
        // If the prefix is S1
        if (S1 == prefix)
            count++;
 
        // If the suffix is S2
        if (S2 == suffix)
            totalcount += count;
    }
 
    // Return the count of substrings
    return totalcount;
}
 
// Function to check if the number of
// substrings starts with S1 and ends
// with S2 and vice-versa is same or not
void checkSubstrings(string S, string S1,
                     string S2)
{
 
    // Count the number of substrings
    int x = countSubstrings(S, S1, S2);
    int y = countSubstrings(S, S2, S1);
 
    // Print the result
    if (x == y)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    string S = "opencloseopencloseopen";
    string S1 = "open";
    string S2 = "close";
    checkSubstrings(S, S1, S2);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to count number of
// substrings that starts with
// string S1 and ends with string S2
static int countSubstrings(String S, String S1,
                           String S2)
{
     
    // Stores the length of each string
    int N = S.length();
    int N1 = S1.length();
    int N2 = S2.length();
 
    // Stores the count of prefixes
    // as S1 and suffixes as S2
    int count = 0, totalcount = 0;
 
    // Traverse string S
    for(int i = 0; i < N; i++)
    {
         
        // Find the prefix at index i
        String prefix = S.substring(
            i, (i + N1 < N) ? (i + N1) : N);
 
        // Find the suffix at index i
        String suffix = S.substring(
            i, (i + N2 < N) ? (i + N2) : N);
 
        // If the prefix is S1
        if (S1.equals(prefix))
            count++;
 
        // If the suffix is S2
        if (S2.equals(suffix))
            totalcount += count;
    }
 
    // Return the count of substrings
    return totalcount;
}
 
// Function to check if the number of
// substrings starts with S1 and ends
// with S2 and vice-versa is same or not
static void checkSubstrings(String S, String S1,
                            String S2)
{
 
    // Count the number of substrings
    int x = countSubstrings(S, S1, S2);
 
    int y = countSubstrings(S, S2, S1);
 
    // Print the result
    if (x == y)
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "opencloseopencloseopen";
    String S1 = "open";
    String S2 = "close";
     
    checkSubstrings(S, S1, S2);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to count number of
# substrings that starts with
# string S1 and ends with string S2
def countSubstrings(S, S1, S2):
     
    # Stores the length of each string
    N = len(S)
    N1 = len(S1)
    N2 = len(S2)
     
    # Stores the count of prefixes
    # as S1 and suffixes as S2
    count = 0
    totalcount = 0
     
    # Traverse string S
    for i in range(N):
         
        # Find the prefix at index i
        prefix = S[i: (i + N1) if (i + N1 < N) else N]
         
        # Find the suffix at index i
        suffix = S[i: (i + N2) if (i + N2 < N) else N]
         
        # If the prefix is S1
        if S1 == prefix:
            count += 1
             
        # If the suffix is S2
        if S2 == suffix:
            totalcount += count
             
    # Return the count of substrings
    return totalcount
 
# Function to check if the number of
# substrings starts with S1 and ends
# with S2 and vice-versa is same or not
def checkSubstrings(S, S1, S2):
     
    x = countSubstrings(S, S1, S2)
    y = countSubstrings(S, S2, S1)
 
    if x == y:
        print("Yes")
    else:
        print("No")
 
# Driver code
S = "opencloseopencloseopen"
S1 = "open"
S2 = "close"
 
checkSubstrings(S, S1, S2)
 
# This code is contributed by abhinavjain194


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count number of
// substrings that starts with
// string S1 and ends with string S2
static int countSubstrings(string S, string S1,
                           string S2)
{
     
    // Stores the length of each string
    int N = S.Length;
    int N1 = S1.Length;
    int N2 = S2.Length;
 
    // Stores the count of prefixes
    // as S1 and suffixes as S2
    int count = 0, totalcount = 0;
 
    // Traverse string S
    for(int i = 0; i < N; i++)
    {
         
        // Find the prefix at index i
        String prefix = S.Substring(
            i, (i + N1 < N) ? N1 : (N - i));
 
        // Find the suffix at index i
        String suffix = S.Substring(
            i, (i + N2 < N) ? N2 : (N - i));
 
        // If the prefix is S1
        if (S1.Equals(prefix))
            count++;
 
        // If the suffix is S2
        if (S2.Equals(suffix))
            totalcount += count;
    }
 
    // Return the count of substrings
    return totalcount;
}
 
// Function to check if the number of
// substrings starts with S1 and ends
// with S2 and vice-versa is same or not
static void checkSubstrings(string S, string S1,
                            string S2)
{
 
    // Count the number of substrings
    int x = countSubstrings(S, S1, S2);
 
    int y = countSubstrings(S, S2, S1);
 
    // Print the result
    if (x == y)
        Console.Write("Yes");
    else
        Console.Write("No");
}
 
// Driver code
static void Main()
{
    string S = "opencloseopencloseopen";
    string S1 = "open";
    string S2 = "close";
     
    checkSubstrings(S, S1, S2);
}
}
 
// This code is contributed by abhinavjain194


Javascript


输出
Yes

时间复杂度: O(N * (N1 + N2)),其中 N、N1 和 N2 分别是字符串S、S1 和 S2 的长度。
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live