📌  相关文章
📜  在给定字符串每次出现子字符串 Y 之前计算子字符串 X 的出现次数

📅  最后修改于: 2021-09-06 11:25:15             🧑  作者: Mango

给定三个分别由NAB字符组成的字符串SXY ,任务是在给定的字符串S 中每次出现子串Y之前找到子串X出现的次数。

例子:

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

  • 初始化一个变量,比如count ,它存储X的总出现次数。
  • 遍历给定的字符串S并执行以下步骤:
    • 如果[i, B]范围内的子字符串等于Y ,则将count增加1
    • 如果子串在范围[I,A]是等于X,然后打印计数的值作为字符串Y的字符串X的当前发生之前得到的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count occurrences of
// the string Y in the string S for
// every occurrence of X in S
void countOccurrences(string S,
                      string X,
                      string Y)
{
    // Stores the count of
    // occurrences of X
    int count = 0;
 
    // Stores the lengths of the
    // three strings
    int N = S.length(), A = X.length();
    int B = Y.length();
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // If the current substring
        // is Y, then increment the
        // value of count by 1
        if (S.substr(i, B) == Y)
            count++;
 
        // If the current substring
        // is X, then print the count
        if (S.substr(i, A) == X)
            cout << count << " ";
    }
}
 
// Driver Code
int main()
{
    string S = "abcdefdefabc";
    string X = "abc";
    string Y = "def";
    countOccurrences(S, X, Y);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to count occurrences of
    // the string Y in the string S for
    // every occurrence of X in S
    static void countOccurrences(String S, String X,
                                 String Y)
    {
        // Stores the count of
        // occurrences of X
        int count = 0;
 
        // Stores the lengths of the
        // three strings
        int N = S.length(), A = X.length();
        int B = Y.length();
 
        // Traverse the string S
        for (int i = 0; i < N; i++) {
 
            // If the current substring
            // is Y, then increment the
            // value of count by 1
            if (S.substring(i, Math.min(N, i + B))
                    .equals(Y))
                count++;
 
            // If the current substring
            // is X, then print the count
            if (S.substring(i, Math.min(N, i + A))
                    .equals(X))
                System.out.print(count + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "abcdefdefabc";
        String X = "abc";
        String Y = "def";
        countOccurrences(S, X, Y);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python program for the above approach
 
# Function to count occurrences of
# the string Y in the string S for
# every occurrence of X in S
def countOccurrences(S, X, Y):
   
    # Stores the count of
    # occurrences of X
    count = 0
 
    # Stores the lengths of the
    # three strings
    N = len(S)
    A = len(X)
    B = len(Y)
 
    # Traverse the string S
    for  i in range( 0,  N):
       
        # If the current substring
        # is Y, then increment the
        # value of count by 1
        if (S[i: i+B] == Y):
            count+=1
 
        # If the current substring
        # is X, then print the count
        if (S[i:i+A] == X):
            print(count, end = " ")
             
# Driver Code
S = "abcdefdefabc"
X = "abc"
Y = "def"
countOccurrences(S, X, Y)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
public class GFG {
 
    // Function to count occurrences of
    // the string Y in the string S for
    // every occurrence of X in S
    static void countOccurrences(string S, string X,
                                 string Y)
    {
        // Stores the count of
        // occurrences of X
        int count = 0;
 
        // Stores the lengths of the
        // three strings
        int N = S.Length, A = X.Length;
        int B = Y.Length;
        int P = Math.Min(A, Math.Min(N, B));
 
        // Traverse the string S
        for (int i = 0; i < N - P + 1; i++) {
 
            // If the current substring
            // is Y, then increment the
            // value of count by 1
 
            if (S.Substring(i, Math.Min(N, B)).Equals(Y))
                count++;
 
            // If the current substring
            // is X, then print the count
            if (S.Substring(i, Math.Min(N, A)).Equals(X))
                Console.Write(count + " ");
        }
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string S = "abcdefdefabc";
        string X = "abc";
        string Y = "def";
        countOccurrences(S, X, Y);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
0 2

时间复杂度: O(N*(A + B))
辅助空间: O(1)

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