查询打印在给定范围内出现最多次数的字符
给定一个大小为N的字符串S和Q个查询。每个查询由L和R ( 0 < = L < = R < N ) 组成。任务是打印在给定范围内出现最多次数的字符。如果有多个字符出现最大次数,则打印按字典顺序排列的最小的字符。
注意: S由小写英文字母组成。
例子:
Input:
str = “geekss”,
Q = 2
0 2
3 5
Output:
e
s
Input:
str = “striver”,
Q = 3
0 1
1 6
5 6
Output:
s
r
e
朴素的方法:一种朴素的方法是对每个查询从L迭代到R ,并使用大小为26的频率数组找到出现次数最多的字符。
时间复杂度:每个查询 O(max(RL), 26)。
高效方法:一种有效的方法是使用前缀和数组来有效地回答查询。让pre[i][j]存储字符j的出现直到第i个索引。对于每个查询出现的字符j将是pre[r][j] – pre[l-1][j] (如果 l > 0)。通过迭代 26 个小写字母,找到出现次数最多的字典最小字符。
下面是上述方法的实现:
C++
// C++ program to find the sum of
// the addition of all possible subsets.
#include
using namespace std;
// Function that answers all the queries
void solveQueries(string str, vector >& query)
{
// Length of the string
int len = str.size();
// Number of queries
int Q = query.size();
// Prefix array
int pre[len][26];
memset(pre, 0, sizeof pre);
// Iterate for all the characters
for (int i = 0; i < len; i++) {
// Increase the count of the character
pre[i][str[i] - 'a']++;
// Presum array for
// all 26 characters
if (i) {
// Update the prefix array
for (int j = 0; j < 26; j++)
pre[i][j] += pre[i - 1][j];
}
}
// Answer every query
for (int i = 0; i < Q; i++) {
// Range
int l = query[i][0];
int r = query[i][1];
int maxi = 0;
char c = 'a';
// Iterate for all characters
for (int j = 0; j < 26; j++) {
// Times the lowercase character
// j occurs till r-th index
int times = pre[r][j];
// Subtract the times it occurred
// till (l-1)th index
if (l)
times -= pre[l - 1][j];
// Max times it occurs
if (times > maxi) {
maxi = times;
c = char('a' + j);
}
}
// Print the answer
cout << "Query " << i + 1 << ": " << c << endl;
}
}
// Driver Code
int main()
{
string str = "striver";
vector > query;
query.push_back({ 0, 1 });
query.push_back({ 1, 6 });
query.push_back({ 5, 6 });
solveQueries(str, query);
}
Java
// Java program to find the sum of
// the addition of all possible subsets.
class GFG
{
// Function that answers all the queries
static void solveQueries(String str,
int[][] query)
{
// Length of the string
int len = str.length();
// Number of queries
int Q = query.length;
// Prefix array
int[][] pre = new int[len][26];
// Iterate for all the characters
for (int i = 0; i < len; i++)
{
// Increase the count of the character
pre[i][str.charAt(i) - 'a']++;
// Presum array for
// all 26 characters
if (i > 0)
{
// Update the prefix array
for (int j = 0; j < 26; j++)
pre[i][j] += pre[i - 1][j];
}
}
// Answer every query
for (int i = 0; i < Q; i++)
{
// Range
int l = query[i][0];
int r = query[i][1];
int maxi = 0;
char c = 'a';
// Iterate for all characters
for (int j = 0; j < 26; j++)
{
// Times the lowercase character
// j occurs till r-th index
int times = pre[r][j];
// Subtract the times it occurred
// till (l-1)th index
if (l > 0)
times -= pre[l - 1][j];
// Max times it occurs
if (times > maxi)
{
maxi = times;
c = (char)('a' + j);
}
}
// Print the answer
System.out.println("Query" + (i + 1) + ": " + c);
}
}
// Driver Code
public static void main(String[] args)
{
String str = "striver";
int[][] query = {{0, 1}, {1, 6}, {5, 6}};
solveQueries(str, query);
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 program to find the sum of
# the addition of all possible subsets.
# Function that answers all the queries
def solveQueries(Str, query):
# Length of the String
ll = len(Str)
# Number of queries
Q = len(query)
# Prefix array
pre = [[0 for i in range(256)]
for i in range(ll)]
# memset(pre, 0, sizeof pre)
# Iterate for all the characters
for i in range(ll):
# Increase the count of the character
pre[i][ord(Str[i])] += 1
# Presum array for
# all 26 characters
if (i):
# Update the prefix array
for j in range(256):
pre[i][j] += pre[i - 1][j]
# Answer every query
for i in range(Q):
# Range
l = query[i][0]
r = query[i][1]
maxi = 0
c = 'a'
# Iterate for all characters
for j in range(256):
# Times the lowercase character
# j occurs till r-th index
times = pre[r][j]
# Subtract the times it occurred
# till (l-1)th index
if (l):
times -= pre[l - 1][j]
# Max times it occurs
if (times > maxi):
maxi = times
c = chr(j)
# Print the answer
print("Query ", i + 1, ": ", c)
# Driver Code
Str = "striver"
query = [[0, 1], [1, 6], [5, 6]]
solveQueries(Str, query)
# This code is contributed by Mohit Kumar
C#
// C# program to find the sum of
// the addition of all possible subsets.
using System;
class GFG
{
// Function that answers all the queries
static void solveQueries(String str,
int[,] query)
{
// Length of the string
int len = str.Length;
// Number of queries
int Q = query.GetLength(0);
// Prefix array
int[,] pre = new int[len, 26];
// Iterate for all the characters
for (int i = 0; i < len; i++)
{
// Increase the count of the character
pre[i, str[i] - 'a']++;
// Presum array for
// all 26 characters
if (i > 0)
{
// Update the prefix array
for (int j = 0; j < 26; j++)
pre[i, j] += pre[i - 1, j];
}
}
// Answer every query
for (int i = 0; i < Q; i++)
{
// Range
int l = query[i, 0];
int r = query[i, 1];
int maxi = 0;
char c = 'a';
// Iterate for all characters
for (int j = 0; j < 26; j++)
{
// Times the lowercase character
// j occurs till r-th index
int times = pre[r, j];
// Subtract the times it occurred
// till (l-1)th index
if (l > 0)
times -= pre[l - 1, j];
// Max times it occurs
if (times > maxi)
{
maxi = times;
c = (char)('a' + j);
}
}
// Print the answer
Console.WriteLine("Query" + (i + 1) + ": " + c);
}
}
// Driver Code
public static void Main(String[] args)
{
String str = "striver";
int[,] query = {{0, 1}, {1, 6}, {5, 6}};
solveQueries(str, query);
}
}
// This code is contributed by Princi Singh
Javascript
输出:
Query 1: s
Query 2: r
Query 3: e