Queries to find the count of characters preceding the given location | Javascript implementation of the approach ; returns character at index pos - 1 ; Driver code
function Count ( s , pos ) { let c = s [ pos - 1 ] ; let counter = 0 ; for ( let i = 0 ; i < pos - 1 ; i ++ ) { if ( s [ i ] == c ) counter = counter + 1 ; } return counter ; } let s = " " ; let pos ; let n = s . length ; let query = [ 9 , 3 , 2 ] ; let Q = query . length ; for ( let i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; document . write ( Count ( s , pos ) + " " ) ; }