Index of character depending on frequency count in string
Last Updated :
29 Dec, 2022
Given a string str containing only lowercase characters, the task is to answer Q queries of the following type:
- 1 C X: Find the largest i such that str[0…i] has exactly X occurrence of the character C.
- 2 C X: Find the smallest i such that str[0…i] has exactly X occurrence of the character C.
Example:
Input: str = “geeksforgeeks”, query[] = {{1, ‘e’, 2}, {2, ‘k’, 2}}
Output:
8
11
Query 1: “geeksforg” is the largest substring starting at str[0] with ‘e’ appearing exactly twice and the index of the last character is 8.
Query 2: “geeksforgeek” is the smallest substring starting at str[0] with ‘k’ appearing exactly twice and the index of the last character is 11.
Input: str = “abcdabcd”, query[] = {{1, ‘a’, 1}, {2, ‘a’, 2}}
Output:
3
4
Approach: Create two 2-dimensional arrays L[][] and F[][] such that L[i][j] stores the largest i such that the ith character appears exactly jth times in str[0…i] and F[i][j] stores the smallest i such that the ith character appears exactly jth times in str[0…i]. In order to do so, traverse the whole string and maintain a frequency array so that for each iteration/character, its count is updated and then start another loop from 0 to 26 (each letter a-z). In the inner loop, if the iterator is equal to character value then update L[][] and F[][] array with the current index position using outer loop iterator otherwise just increment the L[][] array value for other characters by 1 as only index has been incremented and the character has not occurred. Now, type 1 query can be answered as L[given character][Frequency count] and type 2 query as F[given character][Frequency count].
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 26;
void performQueries(string str, int q, int type[],
char ch[], int freq[])
{
int n = str.length();
int L[MAX][n];
int F[MAX][n];
int cnt[MAX] = { 0 };
for ( int i = 0; i < n; i++) {
int k = str[i] - 'a' ;
cnt[k]++;
for ( int j = 0; j < MAX; j++) {
if (k == j) {
L[j][cnt[j]] = i;
F[j][cnt[j]] = i;
}
else
L[j][cnt[j]] = L[j][cnt[j]] + 1;
}
}
for ( int i = 0; i < q; i++) {
if (type[i] == 1) {
cout << L[ch[i] - 'a' ][freq[i]];
}
else {
cout << F[ch[i] - 'a' ][freq[i]];
}
cout << "\n" ;
}
}
int main()
{
string str = "geeksforgeeks" ;
int type[] = { 1, 2 };
char ch[] = { 'e' , 'k' };
int freq[] = { 2, 2 };
int q = sizeof (type) / sizeof ( int );
performQueries(str, q, type, ch, freq);
return 0;
}
|
Java
class GFG
{
static int MAX = 26 ;
static void performQueries(String str, int q, int type[],
char ch[], int freq[])
{
int n = str.length();
int [][]L = new int [MAX][n];
int [][]F = new int [MAX][n];
int []cnt = new int [MAX];
for ( int i = 0 ; i < n; i++)
{
int k = str.charAt(i) - 'a' ;
cnt[k]++;
for ( int j = 0 ; j < MAX; j++)
{
if (k == j)
{
L[j][cnt[j]] = i;
F[j][cnt[j]] = i;
}
else
L[j][cnt[j]] = L[j][cnt[j]] + 1 ;
}
}
for ( int i = 0 ; i < q; i++)
{
if (type[i] == 1 )
{
System.out.print(L[ch[i] - 'a' ][freq[i]]);
}
else
{
System.out.print(F[ch[i] - 'a' ][freq[i]]);
}
System.out.print( "\n" );
}
}
public static void main(String []args)
{
String str = "geeksforgeeks" ;
int type[] = { 1 , 2 };
char ch[] = { 'e' , 'k' };
int freq[] = { 2 , 2 };
int q = type.length;
performQueries(str, q, type, ch, freq);
}
}
|
Python3
import numpy as np
MAX = 26 ;
def performQueries(string , q, type_arr, ch, freq) :
n = len (string);
L = np.zeros(( MAX , n));
F = np.zeros(( MAX , n));
cnt = [ 0 ] * MAX ;
for i in range (n) :
k = ord (string[i]) - ord ( 'a' );
cnt[k] + = 1 ;
for j in range ( MAX ) :
if (k = = j) :
L[j][cnt[j]] = i;
F[j][cnt[j]] = i;
else :
L[j][cnt[j]] = L[j][cnt[j]] + 1 ;
for i in range (q) :
if (type_arr[i] = = 1 ) :
print (L[ ord (ch[i]) -
ord ( 'a' )][freq[i]], end = "");
else :
print (F[ ord (ch[i]) -
ord ( 'a' )][freq[i]], end = "");
print ()
if __name__ = = "__main__" :
string = "geeksforgeeks" ;
type_arr = [ 1 , 2 ];
ch = [ 'e' , 'k' ];
freq = [ 2 , 2 ];
q = len (type_arr);
performQueries(string, q, type_arr, ch, freq);
|
C#
using System;
class GFG
{
static int MAX = 26;
static void performQueries(String str, int q, int []type,
char []ch, int []freq)
{
int n = str.Length;
int [,]L = new int [MAX, n];
int [,]F = new int [MAX, n];
int []cnt = new int [MAX];
for ( int i = 0; i < n; i++)
{
int k = str[i] - 'a' ;
cnt[k]++;
for ( int j = 0; j < MAX; j++)
{
if (k == j)
{
L[j, cnt[j]] = i;
F[j, cnt[j]] = i;
}
else
L[j, cnt[j]] = L[j, cnt[j]] + 1;
}
}
for ( int i = 0; i < q; i++)
{
if (type[i] == 1)
{
Console.Write(L[ch[i] - 'a' , freq[i]]);
}
else
{
Console.Write(F[ch[i] - 'a' , freq[i]]);
}
Console.Write( "\n" );
}
}
public static void Main(String []args)
{
String str = "geeksforgeeks" ;
int []type = { 1, 2 };
char []ch = { 'e' , 'k' };
int []freq = { 2, 2 };
int q = type.Length;
performQueries(str, q, type, ch, freq);
}
}
|
Javascript
<script>
let MAX = 26;
function performQueries(str, q, type, ch, freq)
{
let n = str.length;
let L = new Array(MAX);
let F = new Array(MAX);
let cnt = new Array(MAX);
for (let i = 0; i < MAX; i++)
{
L[i] = new Array(n);
F[i] = new Array(n);
cnt[i] = 0;
for (let j = 0; j < n; j++)
{
L[i][j] = 0;
F[i][j] = 0;
}
}
for (let i = 0; i < n; i++)
{
let k = str[i].charCodeAt() - 'a' .charCodeAt();
cnt[k]++;
for (let j = 0; j < MAX; j++)
{
if (k == j)
{
L[j][cnt[j]] = i;
F[j][cnt[j]] = i;
}
else
L[j][cnt[j]] = L[j][cnt[j]] + 1;
}
}
for (let i = 0; i < q; i++)
{
if (type[i] == 1)
{
document.write(L[ch[i].charCodeAt() -
'a' .charCodeAt()][freq[i]]);
}
else
{
document.write(F[ch[i].charCodeAt() -
'a' .charCodeAt()][freq[i]]);
}
document.write( "</br>" );
}
}
let str = "geeksforgeeks" ;
let type = [ 1, 2 ];
let ch = [ 'e' , 'k' ];
let freq = [ 2, 2 ];
let q = type.length;
performQueries(str, q, type, ch, freq);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(26 * n)
Share your thoughts in the comments
Please Login to comment...