Find the number of occurrences of a character upto preceding position
Last Updated :
21 Dec, 2022
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 position P up to 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 4
Input : S = “geeksforgeeks”, P = 9
Output : 1
Naive Approach:A naive approach is to iterate over the string till Position-1 searches for a similar character. Whenever a similar character occurs increment the counter variable by one.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
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 count;
}
int main()
{
string s = "ababababab" ;
int p = 9;
cout << Occurrence(s, p);
return 0;
}
|
Java
class GFG
{
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 count;
}
public static void main(String[] args)
{
String s = "ababababab" ;
int p = 9 ;
System.out.println(Occurrence(s, p));
}
}
|
Python3
def Occurrence(s, position):
count = 0
for i in range (position - 1 ):
if (s[i] = = s[position - 1 ]):
count + = 1
return count
s = "ababababab" ;
p = 9
print (Occurrence(s, p))
|
C#
using System;
class GFG
{
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 count;
}
public static void Main(String[] args)
{
String s = "ababababab" ;
int p = 9;
Console.WriteLine(Occurrence(s, p));
}
}
|
Javascript
<script>
function Occurrence(s,position)
{
let count = 0;
for (let i = 0; i < position - 1; i++)
if (s[i] == s[position - 1])
count++;
return count;
}
let s = "ababababab" ;
let p = 9;
document.write(Occurrence(s, p));
</script>
|
Time Complexity: O(N) for each query.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
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++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def countOccurrence(s, position):
alpha = [ 0 ] * 26
b = [ 0 ] * len (s)
for i in range ( 0 , len (s)):
b[i] = alpha[ ord (s[i]) - 97 ]
alpha[ ord (s[i]) - 97 ] = alpha[ ord (s[i]) - 97 ] + 1
return b[position - 1 ]
s = "ababababab"
p = 9
print (countOccurrence(s, p))
|
C#
using System;
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[i] - 97];
alpha[( int )s[i] - 97]++;
}
return b[position - 1];
}
public static void Main(String[] args)
{
String s = "ababababab" ;
int p = 9;
Console.WriteLine(countOccurrence(s, p));
}
}
|
Javascript
<script>
function countOccurrence(s, position)
{
let alpha = new Array(26);
for (let i = 0; i < 26; i++)
{
alpha[i] = 0;
}
let b = new Array(s.length);
for (let i = 0; i < s.length; i++)
{
b[i] = alpha[s[i].charCodeAt(0) - 97];
alpha[s[i].charCodeAt(0) - 97]++;
}
return b[position - 1];
}
let s = "ababababab" ;
p = 9;
document.write(countOccurrence(s, p));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(26) ? O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...