📌  相关文章
📜  对字符串S中的子字符串对进行计数,以使每对中的S1在S2之后都不会出现

📅  最后修改于: 2021-05-04 16:29:39             🧑  作者: Mango

给定三个字符串strstr1str2 ,任务是将str1str2出现的对数作为字符串str中的子字符串进行计数,以使每对字符串中str1的起始索引均小于或等于str2

例子:

方法:这个想法是在字符串str中找到子字符串str2的起始索引,并且在字符串str2的每个起始索引处,将对数增加str1的起始索引数,直到str的i索引。最后,打印获得的对的总数。请按照以下步骤解决问题:

  • 初始化变量,说cntPairs,以存储对str1STR2的的计数,使得STR1的起始索引小于或等于STR2。
  • 初始化一个变量cntStr1 ,以将子字符串str1的计数存储在起始索引小于或等于i的str中
  • [0,N – 1]范围内迭代。对于字符串str的每个i索引,检查子字符串str1是否存在于字符串str中,其起始索引等于i 。如果确定为true,则将cntStr1的值增加1
  • 对于每个i索引,检查子字符串str2是否存在于起始索引等于i的字符串。如果发现为true,则将cntPairs的值增加cntStr1
  • 最后,打印cntPairs的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the count of pairs of str1
// and str2 in str such that starting index of
// str1 less than or equal to str2
int CountSubstring(string str, string str1, string str2)
{
 
    // Stores count of pairs of str1 and str2
    // such that the start index of str1 is
    // less than or equal to str2
    int cntPairs = 0;
 
    // Stores count of substring str1 in
    // str whose start index is up to i
    int cntStr1 = 0;
 
    // Iterate over the range [0, N -1]
    for (int i = 0; i < str.length(); i++) {
 
        // If substring str1 whose
        // start index is i
        if (str.substr(i, str1.length())
            == str1) {
 
            // Update cntStr1
            cntStr1++;
        }
 
        // If substring str2 whose
        // start index is i
        else if (str.substr(i, str2.length())
                 == str2) {
 
            // Update cntPairs
            cntPairs += cntStr1;
        }
    }
 
    return cntPairs;
}
 
// Driver Code
int main()
{
 
    string str = "geeksforgeeksfor";
    string str1 = "geeks", str2 = "for";
 
    cout << CountSubstring(str, str1, str2);
 
    return 0;
}


Java
// Java program to implement
// the above approach
public class GFG {
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = new String("geeksforgeeksfor");
        String str1 = new String("geeks");
        String str2 = new String("for");
 
        System.out.println(CountSubstring(
            str, str1, str2));
    }
 
    // Function to find the count of pairs of str1
    // and str2 in str such that starting index of
    // str1 less than or equal to str2
    static int CountSubstring(String str,
                              String str1,
                              String str2)
    {
 
        // Stores count of pairs of str1 and str2
        // such that the start index of str1 is
        // less than or equal to str2
        int cntPairs = 0;
 
        // Stores count of substring str1 in
        // str whose start index is up to i
        int cntStr1 = 0;
 
        // Stores length of str
        int N = str.length();
 
        // Stores length of str1
        int s1 = str1.length();
 
        // Stores length of str2
        int s2 = str2.length();
 
        // Iterate over the range [0, N -1]
        for (int i = 0; i < N; i++) {
 
            // If substring str1 whose
            // starting index is i
            if (((i + s1) <= N)
                && (str.substring(
                        i, (i + s1)))
                           .compareTo(str1)
                       == 0) {
 
                // Update cntStr1
                cntStr1++;
            }
 
            // If substring str2 whose
            // starting index is i
            else if (((i + s2) <= N)
                     && (str.substring(
                             i, (i + s2)))
                                .compareTo(str2)
                            == 0) {
 
                // Update cntPairs
                cntPairs += cntStr1;
            }
        }
 
        return cntPairs;
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the count of pairs of str1
# and str2 in str such that starting index of
# str1 less than or equal to str2
def CountSubstring(str, str1, str2):
     
    # Stores count of pairs of str1 and str2
    # such that the start index of str1 is
    # less than or equal to str2
    cntStr1 = 0
     
    # Stores count of substring str1 in
    # str whose start index is up to i
    cntPairs = 0
     
    # Iterate over the range [0, N -1]
    for i in range(len(str)):
         
        # If substring str1 whose
        # start index is i
        if str[i : i + len(str1)] == str1:
             
            # Update cntStr1
            cntStr1 += 1
             
        # If substring str2 whose
        # start index is i   
        elif str[i : i + len(str2)] == str2:
             
            # Update cntPairs
            cntPairs += cntStr1
     
    return cntPairs
     
# Driver Code
str = 'geeksforgeeksfor'
str1 = 'geeks'
str2 = 'for'       
        
print(CountSubstring(str, str1, str2))       
 
# This code is contributed by dharanendralv23


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
   
// Function to find the count of pairs of str1
// and str2 in str such that starting index of
// str1 less than or equal to str2 
static int CountSubstring(String str,
                          String str1,
                          String str2)
{
     
    // Stores count of pairs of str1 and str2
    // such that the start index of str1 is
    // less than or equal to str2
    int cntPairs = 0;
 
    // Stores count of substring str1 in
    // str whose start index is up to i
    int cntStr1 = 0;
 
    // Stores length of str
    int N = str.Length;
 
    // Stores length of str1
    int s1 = str1.Length;
 
    // Stores length of str2
    int s2 = str2.Length;
 
    // Iterate over the range [0, N -1]
    for(int i = 0; i < N; i++)
    {
         
        // If substring str1 whose
        // starting index is i
        if (((i + s1) <= N) &&
            (str.Substring(i, s1)).Equals(str1))
        {
             
            // Update cntStr1
            cntStr1++;
        }
 
        // If substring str2 whose
        // starting index is i
        else if (((i + s2) <= N) &&
                 (str.Substring(i, s2)).Equals(str2))
        {
             
            // Update cntPairs
            cntPairs += cntStr1;
        }
    }
    return cntPairs;
}
 
// Driver code
static public void Main()
{
    String str = "geeksforgeeksfor";        
    String str1 = "geeks";   
    String str2 = "for";
     
    Console.Write(CountSubstring(str, str1, str2));        
}
}
 
// This code is contributed by dharanendralv23


输出:
3

时间复杂度: O(| str | *(| str1 | + | str2 |))
辅助空间: O(1)