📌  相关文章
📜  用于计算给定索引范围内给定字符的频率的查询

📅  最后修改于: 2021-09-07 02:25:17             🧑  作者: Mango

给定长度为N的字符串S和形式为{l, r, y}的查询数组Q[][] 。对于每个查询,任务是打印范围[l, r] 中存在的字符数y

例子:

朴素方法:最简单的方法是在[l, r]范围内遍历字符串如果每个查询{l, r, y} 的索引i处的字符等于y ,则将计数器加1 。遍历后,打印每个查询的计数器。

时间复杂度: O(N*Q)
辅助空间: O(N)

有效的方法:这个想法是使用前缀和技术预先计算从‘a’‘z’ 的每个字符在1 到 i范围内的字符数,其中1 ≤ i ≤ N。请按照以下步骤解决问题:

  1. 初始化一个数组dp[N+1][26] ,其中dp[i][j]存储存在于[0, i]范围内的字符数(i+’a’)
  2. 现在,通过将dp[i][j]递增dp[i – 1][j]其中0 ≤ j < 26并递增,预先计算范围[1, i]中存在的每个字符的数量,其中1 ≤ i ≤ N dp[i][S[j] – ‘a’]乘以1
  3. 对于每个查询{l, r, y} ,打印dp[r][y – ‘a’] – dp[l – 1][y – ‘a’] 的值作为范围内出现的字符数y [l, r]

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to print count of char
// y present in the range [l, r]
void noOfChars(string s, char queries[][3], int q)
{
   
    // Length of the string
    int n = s.length();
     
    // Stores the precomputed results
    int dp[n + 1][26];
    memset(dp, 0, sizeof(dp));
   
    // Iterate the given string
    for(int i = 0; i < n; i++)
    {
       
         // Increment dp[i][y-'a'] by 1
        dp[i + 1][s[i]-'a']++;
       
        // Pre-compute
        for(int j = 0; j < 26; j++)
        {
            dp[i + 1][j] += dp[i][j];
        }
         
    }
   
    // Traverse each query
    for(int i = 0; i < q; i++)
    {
        int l = (int)queries[i][0];
        int r = (int)queries[i][1];
        int c = queries[i][2] - 'a';
       
        // Print the result for
        // each query
        cout << dp[r] - dp[l - 1] << " ";
    }
}
 
// Driver Code
int main()
{
   
    // Given string
    string S = "aabv";
   
    // Given Queries
    char queries[2][3] = {{ 1, 2, 'a' },{ 2, 3, 'b' }};
       
    // Function Call
    noOfChars(S, queries, 2);
    return 0;
}
 
// This code is contributed by avanitrachhadiya2155


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to print count of char
    // y present in the range [l, r]
    static void noOfChars(String s,
                          char[][] queries)
    {
 
        // Length of the string
        int n = s.length();
 
        // Stores the precomputed results
        int dp[][] = new int[n + 1][26];
 
        // Iterate the given string
        for (int i = 0; i < n; i++) {
 
            // Increment dp[i][y-'a'] by 1
            dp[i + 1][s.charAt(i) - 'a']++;
 
            // Pre-compute
            for (int j = 0; j < 26; j++) {
                dp[i + 1][j] += dp[i][j];
            }
        }
 
        // Number of queries
        int q = queries.length;
 
        // Traverse each query
        for (int i = 0; i < q; i++) {
 
            int l = (int)queries[i][0];
            int r = (int)queries[i][1];
            int c = queries[i][2] - 'a';
 
            // Print the result for
            // each query
            System.out.print(
                dp[r] - dp[l - 1]
                + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given string
        String S = "aabv";
 
        // Given Queries
        char queries[][]
            = new char[][] { { 1, 2, 'a' },
                             { 2, 3, 'b' } };
 
        // Function Call
        noOfChars(S, queries);
    }
}


Python3
# Python3 program for the above approach
 
# Function to prcount of char
# y present in the range [l, r]
def noOfChars(s, queries):
     
    # Length of the string
    n = len(s)
 
    # Stores the precomputed results
    dp = [[0 for i in range(26)]
             for i in range(n + 1)]
 
    # Iterate the given string
    for i in range(n):
 
        # Increment dp[i][y-'a'] by 1
        dp[i + 1][ord(s[i]) - ord('a')] += 1
 
        # Pre-compute
        for j in range(26):
            dp[i + 1][j] += dp[i][j]
 
    # Number of queries
    q = len(queries)
 
    # Traverse each query
    for i in range(q):
        l = queries[i][0]
        r = queries[i][1]
        c = ord(queries[i][2]) - ord('a')
 
        # Print the result for
        # each query
        print(dp[r] - dp[l - 1], end = " ")
         
# Driver Code
if __name__ == '__main__':
     
    # Given string
    S = "aabv"
 
    # Given Queries
    queries = [ [ 1, 2, 'a' ],
                [ 2, 3, 'b' ] ]
 
    # Function Call
    noOfChars(S, queries)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
public class GFG {
 
    // Function to print count of char
    // y present in the range [l, r]
    static void noOfChars(String s,
                          char[,] queries)
    {
 
        // Length of the string
        int n = s.Length;
 
        // Stores the precomputed results
        int [,]dp = new int[n + 1, 26];
 
        // Iterate the given string
        for (int i = 0; i < n; i++) {
 
            // Increment dp[i,y-'a'] by 1
            dp[i + 1, s[i] - 'a']++;
 
            // Pre-compute
            for (int j = 0; j < 26; j++) {
                dp[i + 1, j] += dp[i, j];
            }
        }
 
        // Number of queries
        int q = queries.GetLength(0);
 
        // Traverse each query
        for (int i = 0; i < q; i++) {
 
            int l = (int)queries[i, 0];
            int r = (int)queries[i, 1];
            int c = queries[i, 2] - 'a';
 
            // Print the result for
            // each query
            Console.Write(
                dp[r, c] - dp[l - 1, c]
                + " ");
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        // Given string
        String S = "aabv";
 
        // Given Queries
        char [,]queries
            = new char[,] { { (char)1, (char)2, 'a' },
                             { (char)2, (char)3, 'b' } };
 
        // Function Call
        noOfChars(S, queries);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2 1

时间复杂度: O(Q+N*26)
辅助空间: O(N)

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