Find the number of occurrences of a character upto preceding position | CPP program to find the number of occurrences of a character at position P upto p - 1 ; Function to find the number of occurrences of a character at position P upto p - 1 ; Iterate over the string ; Store the Occurrence of same character ; Increase its frequency ; Return the required count ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOccurrence ( string s , int position ) { int alpha [ 26 ] = { 0 } , b [ s . size ( ) ] = { 0 } ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { b [ i ] = alpha [ ( int ) s [ i ] - 97 ] ; alpha [ ( int ) s [ i ] - 97 ] ++ ; } return b [ position - 1 ] ; } int main ( ) { string s = " ababababab " ; int p = 9 ; cout << countOccurrence ( s , p ) ; return 0 ; }