Find the number of occurrences of a character upto preceding position | Java 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
import java . util . * ; class GFG { static int countOccurrence ( String s , int position ) { int [ ] alpha = new int [ 26 ] ; int [ ] b = new int [ s . length ( ) ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { b [ i ] = alpha [ ( int ) s . charAt ( i ) - 97 ] ; alpha [ ( int ) s . charAt ( i ) - 97 ] ++ ; } return b [ position - 1 ] ; } public static void main ( String [ ] args ) { String s = " ababababab " ; int p = 9 ; System . out . println ( countOccurrence ( s , p ) ) ; } }