Given a string S of length N and an integer P(1≤P≤N) denoting the position of a character in the string. The task is to find the number of occurrences of the character present at the position P upto P-1 index.
Examples:
Input : S = “ababababab”, P = 9
Output : 4
Character at P is ‘a’. Number of occurrences of ‘a’ upto 8th index is 4Input : S = “geeksforgeeks”, P = 9
Output : 1
Naive Approach :A naive approach is to iterate over the string till Position-1 searching for a similar character. Whenever a similar character occurs increment counter variable by one.
Below is the implementation of the above approach:
C++
// CPP program to find the number of occurrences // of a character at position P upto p-1 #include <bits/stdc++.h> using namespace std; // Function to find the number of occurrences // of a character at position P upto p-1 int Occurrence(string s, int position) { int count = 0; for (int i = 0; i < position - 1; i++) if (s[i] == s[position - 1]) count++; // Return the required count return count; } // Driver code int main() { string s = "ababababab"; int p = 9; // Function call cout << Occurrence(s, p); return 0; } |
Java
// Java program to find the number of occurrences // of a character at position P upto p-1 class GFG { // Function to find the number of occurrences // of a character at position P upto p-1 static int Occurrence(String s, int position) { int count = 0; for (int i = 0; i < position - 1; i++) if (s.charAt(i) == s.charAt(position - 1)) count++; // Return the required count return count; } // Driver code public static void main(String[] args) { String s = "ababababab"; int p = 9; // Function call System.out.println(Occurrence(s, p)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 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 def Occurrence(s, position): count = 0 for i in range(position - 1): if (s[i] == s[position - 1]): count += 1 # Return the required count return count # Driver code s = "ababababab"; p = 9 # Function call print(Occurrence(s, p)) # This code is contributed by Mohit Kumar |
C#
// C# program to find the number of occurrences // of a character at position P upto p-1 using System; class GFG { // Function to find the number of occurrences // of a character at position P upto p-1 static int Occurrence(String s, int position) { int count = 0; for (int i = 0; i < position - 1; i++) if (s[i] == s[position - 1]) count++; // Return the required count return count; } // Driver code public static void Main(String[] args) { String s = "ababababab"; int p = 9; // Function call Console.WriteLine(Occurrence(s, p)); } } // This code is contributed by PrinciRaj1992 |
4
Time Complexity : O(N) for each query.
Efficient Approach: In case, if there are multiple such queries and we are given a unique index P for every query then an efficient approach is to use a frequency array for storing the character count in each iteration of the string.
Below is the implementation of the above approach :
C++
// CPP program to find the number of occurrences // of a character at position P upto p-1 #include <bits/stdc++.h> using namespace std; // Function to find the number of occurrences // of a character at position P upto p-1 int countOccurrence(string s, int position) { int alpha[26] = { 0 }, b[s.size()] = { 0 }; // Iterate over the string for (int i = 0; i < s.size(); i++) { // Store the Occurrence of same character b[i] = alpha[(int)s[i] - 97]; // Increase its frequency alpha[(int)s[i] - 97]++; } // Return the required count return b[position - 1]; } // Driver code int main() { string s = "ababababab"; int p = 9; // Function call cout << countOccurrence(s, p); return 0; } |
Java
// Java program to find the number of occurrences // of a character at position P upto p-1 import java.util.*; class GFG { // Function to find the number of occurrences // of a character at position P upto p-1 static int countOccurrence(String s, int position) { int []alpha = new int[26]; int []b = new int[s.length()]; // Iterate over the string for (int i = 0; i < s.length(); i++) { // Store the Occurrence of same character b[i] = alpha[(int)s.charAt(i) - 97]; // Increase its frequency alpha[(int)s.charAt(i) - 97]++; } // Return the required count return b[position - 1]; } // Driver code public static void main(String[] args) { String s = "ababababab"; int p = 9; // Function call System.out.println(countOccurrence(s, p)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 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 def countOccurrence(s, position): alpha = [0] * 26 b = [0] * len(s) # Iterate over the string for i in range(0, len(s)): # Store the Occurrence of same character b[i] = alpha[ord(s[i]) - 97] # Increase its frequency alpha[ord(s[i]) - 97] = alpha[ord(s[i]) - 97] + 1 # Return the required count return b[position - 1] # Driver code s = "ababababab" p = 9 # Function call print(countOccurrence(s, p)) # This code is contributed by Sanjit_Prasad |
C#
// C# program to find the number of occurrences // of a character at position P upto p-1 using System; class GFG { // Function to find the number of occurrences // of a character at position P upto p-1 static int countOccurrence(String s, int position) { int []alpha = new int[26]; int []b = new int[s.Length]; // Iterate over the string for (int i = 0; i < s.Length; i++) { // Store the Occurrence of same character b[i] = alpha[(int)s[i] - 97]; // Increase its frequency alpha[(int)s[i] - 97]++; } // Return the required count return b[position - 1]; } // Driver code public static void Main(String[] args) { String s = "ababababab"; int p = 9; // Function call Console.WriteLine(countOccurrence(s, p)); } } // This code is contributed by PrinciRaj1992 |
4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Queries to find the count of characters preceding the given location
- Map every character of one string to another such that all occurrences are mapped to the same character
- Replace all occurrences of character X with character Y in given string
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Print the string by ignoring alternate occurrences of any character
- Check if all occurrences of a character appear together
- Remove all occurrences of a character in a string
- Printing frequency of each character just after its consecutive occurrences
- Minimize ASCII values sum after removing all occurrences of one character
- Count occurrences of a character in a repeated string
- Remove all occurrences of a character in a string | Recursive approach
- Check if frequency of each character is equal to its position in English Alphabet
- Find a string such that every character is lexicographically greater than its immediate next character
- Longest Common Prefix using Character by Character Matching
- Replace every character of string by character whose ASCII value is K times more than it
- Count substrings that starts with character X and ends with character Y
- Replace every character of a string by a different character
- Shortest distance to every other character from given character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Modify the string such that every character gets replaced with the next character in the keyboard
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

