Find the number of occurrences of a character upto preceding position | 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
def countOccurrence ( s , position ) : NEW_LINE INDENT alpha = [ 0 ] * 26 NEW_LINE b = [ 0 ] * len ( s ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT b [ i ] = alpha [ ord ( s [ i ] ) - 97 ] NEW_LINE alpha [ ord ( s [ i ] ) - 97 ] = alpha [ ord ( s [ i ] ) - 97 ] + 1 NEW_LINE DEDENT return b [ position - 1 ] NEW_LINE DEDENT s = " ababababab " NEW_LINE p = 9 NEW_LINE print ( countOccurrence ( s , p ) ) NEW_LINE