Index of character depending on frequency count in string | C # implementation of the approach ; Function to perform the queries ; L [ i , j ] stores the largest i such that ith character appears exactly jth times in str [ 0. . . i ] ; F [ i , j ] stores the smallest i such that ith character appears exactly jth times in str [ 0. . . i ] ; To store the frequency of each of the character of str ; Current character of str ; Update its frequency ; For every lowercase character of the English alphabet ; If it is equal to the character under consideration then update L [ , ] and R [ , ] as it is cnt [ j ] th occurrence of character k ; Only update L [ , ] as k has not been occurred so only index has to be incremented ; Perform the queries ; Type 1 query ; Type 2 query ; Driver code ; Queries ; Perform the queries
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 ( " STRNEWLINE " ) ; } } 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 ) ; } }