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

📅  最后修改于: 2021-05-17 21:42:17             🧑  作者: Mango

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

例子:

天真的方法:最简单的方法是在每个查询{l,r,y}的索引i处的字符等于y的情况下遍历[l,r]范围内的字符串并将计数器增加1 。遍历后,为每个查询打印计数器。

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

高效方法:我们的想法是在范围为1〜预先计算本字符数为i对于其中1≤I≤N使用前缀和技术从“a”“Z”的每个字符。请按照以下步骤解决问题:

  1. 初始化数组dp [N + 1] [26] ,其中dp [i] [j]存储范围为[0,i]的字符数(i +’a’)
  2. 现在,预先计算每个字符存在于该范围中的数目[1,I],其中1≤I≤N乘递增DP [i] [j]DP [I – 1] [j]的其中0≤Ĵ<26和增量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


输出:
2 1

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