📌  相关文章
📜  计算字符串S 中的子字符串对,使得每对中的 S1 不会出现在 S2 之后

📅  最后修改于: 2021-09-06 05:55:47             🧑  作者: Mango

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

例子:

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

  • 初始化变量,说cntPairs,以存储对str1STR2的的计数,使得STR1的起始索引小于或等于STR2。
  • 初始化变量,说cntStr1,存储子串的str中其开始索引是计数,STR1小于或等于至i。
  • 迭代范围[0, N – 1] 。对于字符串str 的每个i索引,检查子字符串str1 是否存在于起始索引等于i的字符串str 中。如果发现为真,则将cntStr1的值增加1
  • 对于每个i索引,检查子字符串str2是否存在于起始索引等于i的字符串。如果发现为真,则将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)

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