📜  查询以查找指定子字符串的频率

📅  最后修改于: 2021-09-03 04:04:39             🧑  作者: Mango

给定一个字符串S和一个查询矩阵Q ,每个查询分别指定S的子字符串的开始和结束索引L( = Q[i][0])R( = Q[i][0]) ,任务是在子串[L, R] 中找到字符串K的频率。

注意:范围遵循基于 1 的索引。

例子:

天真的方法:
为所有查询运行从LR的循环。计算字符串K 的出现次数并返回计数。

时间复杂度: O(Q * |S| 的长度)。

有效的方法:
预先计算并存储每个索引的 K 频率。现在,为了计算[L, R]范围内字符串的频率,我们只需要计算 K 在索引(R-1)(L-1)处的频率之间的差异。

下面是上述方法的实现:

C++
// C++ Program to find
// frequency of a string K
// in a substring [L, R] in S
 
#include 
#define max_len 100005
using namespace std;
 
// Store the frequency of
// string for each index
int cnt[max_len];
 
// Compute and store frequencies
// for every index
void precompute(string s, string K)
{
 
    int n = s.size();
    for (int i = 0; i < n - 1; i++) {
        cnt[i + 1]
            = cnt[i]
              + (s.substr(i, K.size()) == K);
    }
}
 
// Driver Code
int main()
{
    string s = "ABCABCABABC";
    string K = "ABC";
    precompute(s, K);
 
    vector > Q
        = { { 1, 6 }, { 5, 11 } };
 
    for (auto it : Q) {
        cout << cnt[it.second - 1]
                    - cnt[it.first - 1]
             << endl;
    }
 
    return 0;
}


Java
// Java program to find
// frequency of a string K
// in a substring [L, R] in S
class GFG{
     
static int max_len = 100005;
 
// Store the frequency of
// string for each index
static int cnt[] = new int[max_len];
 
// Compute and store frequencies
// for every index
public static void precompute(String s,
                              String K)
{
    int n = s.length();
     
    for(int i = 0; i < n - 2; i++)
    {
        cnt[i + 1] = cnt[i];
        if (s.substring(
            i, i + K.length()).equals(K))
        {
            cnt[i + 1] += 1;
        }
    }
    cnt[n - 2 + 1] = cnt[n - 2];
}
 
// Driver code
public static void main(String[] args)
{
    String s = "ABCABCABABC";
    String K = "ABC";
    precompute(s, K);
   
    int Q[][] = { { 1, 6 }, { 5, 11 } };
     
    for(int it = 0; it < Q.length; it++)
    {
        System.out.println(cnt[Q[it][1] - 1] -
                           cnt[Q[it][0] - 1]);
    }
}
}
 
// This code is contributed by divyesh072019


Python3
# Python3 Program to find
# frequency of a string K
# in a substring [L, R] in S
max_len = 100005
 
# Store the frequency of
# string for each index
cnt = [0] * max_len
 
# Compute and store frequencies
# for every index
def precompute(s, K):
 
    n = len(s)
    for i in range(n - 1):
        cnt[i + 1] = cnt[i]
        if s[i : len(K) + i] == K:
            cnt[i + 1] += 1
 
# Driver Code
if __name__ == "__main__":
 
    s = "ABCABCABABC"
    K = "ABC"
    precompute(s, K)
    Q = [[1, 6], [5, 11]]
 
    for it in Q:
        print(cnt[it[1] - 1] -
              cnt[it[0] - 1])
 
# This code is contributed by Chitranayal


C#
// C# program to find frequency of
// a string K in a substring [L, R] in S
using System.IO;
using System;
 
class GFG{
     
static int max_len = 100005;
 
// Store the frequency of
// string for each index
static int[] cnt = new int[max_len];
 
// Compute and store frequencies
// for every index
static void precompute(string s,string K)
{
    int n = s.Length;
     
    for(int i = 0; i < n - 2; i++)
    {
        cnt[i + 1] = cnt[i];
         
        if (s.Substring(i, K.Length).Equals(K))
        {
            cnt[i + 1] += 1;
        }
    }
    cnt[n - 2 + 1] = cnt[n - 2];
}
 
// Driver code
static void Main()
{
    string s = "ABCABCABABC";
    string K = "ABC";
    precompute(s, K);
    int[,] Q = { { 1, 6 }, { 5, 11 } };
     
    for(int it = 0; it < Q.GetLength(0); it++)
    {  
        Console.WriteLine(cnt[Q[it, 1] - 1] -
                          cnt[Q[it, 0] - 1]);
    }
}
}
 
// This code is contributed by rag2127


Javascript


输出:
2
1

时间复杂度: O( | S | + Q 的长度) ,因为每个查询都在 O(1) 中得到回答。
辅助空间: O( |S| )

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