Skip to content
Related Articles

Related Articles

Check if count of substrings in S with string S1 as prefix and S2 as suffix is equal to that with S2 as prefix and S1 as suffix
  • Last Updated : 06 May, 2021

Given three strings S, S1, and S2, the task is to check if the number of substrings that start and end with S1 and S2 is equal to the number of substrings that start and end with S2 and S1 or not. If found to be true, then print “Yes”. Otherwise, print “No”.

Examples:

Input: S = “helloworldworldhelloworld”, S1 = “hello”, S2 = “world”
Output: No
Explanation:
The substrings that starts with S1 (= “hello”) and ends with S2 (= “world”) are {“helloworld”, “helloworldworld”, “helloworldworldhelloworld”, “helloworld”}. 
Therefore, the total count is 4.
The substrings that starts with S2 (= “world”) and ends with S1(= “hello”) are {“worldworldhello”, “worldhello”}.
Therefore, the total count is 2.
Therefore, the above two counts are not the same.

Input: S = “opencloseopencloseopen”, S1 = “open”, S2 = “close”
Output: Yes

 

Approach: Follow the steps below to solve the problem:

  • Store the length of strings S, S1, and S2 in variables, say N, N1, and N2 respectively.
  • Initialize a variable, say count, to store the number of occurrences of substring S1 in the string S.
  • Initialize a variable, say ans, to store substrings that start and end with S1 and S2 respectively.
  • Traverse the given string S and perform the following steps:
    • Store the substring of string S starting at the index i of size N1 in the string prefix.
    • Store the substring of string S starting at the index i of size N2 in the string suffix.
    • If the prefix is the same as the string S1, then increment the value of count by 1.
    • If the suffix is the same as the string S2, then increment the value of ans by count.
  • Use the above steps, to find the number of substrings that start and end with S2 and S1 respectively. Store the count obtained in the variable say ans2.
  • If the values of ans and ans2 are found to be equal, then print “Yes”. Otherwise, print “No”.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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




<script>
// Javascript program for the above approach
 
// Function to count number of
// substrings that starts with
// string S1 and ends with string S2
function countSubstrings(S, S1, S2)
{
 
    // Stores the length of each string
    let N = S.length;
    let N1 = S1.length;
    let N2 = S2.length;
 
    // Stores the count of prefixes
    // as S1 and suffixes as S2
    let count = 0, totalcount = 0;
 
    // Traverse string S
    for (let i = 0; i < N; i++) {
 
        // Find the prefix at index i
        let prefix = S.substr(i, N1);
 
        // Find the suffix at index i
        let 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
function checkSubstrings(S, S1, S2)
{
 
    // Count the number of substrings
    let x = countSubstrings(S, S1, S2);
    let y = countSubstrings(S, S2, S1);
 
    // Print the result
    if (x == y)
        document.write("Yes");
    else
        document.write("No");
}
 
// Driver Code
    let S = "opencloseopencloseopen";
    let S1 = "open";
    let S2 = "close";
    checkSubstrings(S, S1, S2);
 
// This code is contributed by gfgking
</script>
Output
Yes

Time Complexity: O(N * (N1 + N2)), where N, N1, and N2 are the length of strings S, S1, and S2 respectively.
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :