Queries to find the count of characters preceding the given location | C # implementation of the approach ; returns character at index pos - 1 ; Driver Code
using System ; class GFG { static int Count ( String s , int pos ) { int c = s [ pos - 1 ] ; int counter = 0 ; for ( int i = 0 ; i < pos - 1 ; i ++ ) { if ( s [ i ] == c ) counter = counter + 1 ; } return counter ; } public static void Main ( String [ ] args ) { String s = " abacsddaa " ; int pos ; int n = s . Length ; int [ ] query = { 9 , 3 , 2 } ; int Q = query . Length ; for ( int i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; Console . WriteLine ( Count ( s , pos ) ) ; } } }