Count the characters | Practice | GeeksforGeeks

Given a string S. Count the characters that have ‘N’ number of occurrences. If a character appears consecutively it is counted as 1 occurrence.
Example 1:
Input:
S = "abc", N = 1
Output: 3
Explanation: 'a', 'b' and 'c' all have 
1 occurrence.


â€‹Example 2:
Input: 
S = "geeksforgeeks", N = 2
Output: 4
Explanation: 'g', 'e', 'k' and 's' have
2 occurrences.


Your Task:
You don't need to read input or print anything. Your task is to complete the function getCount() which takes the string S and an integer N as inputs and returns the count of the characters that have exactly N occurrences in the string. Note that the consecutive occurrences of a character have to be counted as 1.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).

Constraints:
1<=|S|<=105
1<=N<=103
 
0
abhishek11102 weeks ago
Java Solution:
int getCount (String s, int N)
    {
        // your code here
        int[] arr = new int[26];
        char prev = '#';
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (prev != c) {
                arr[c - 'a']++;
            }
            prev = c;
        }
        
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == N) {
                count++;
            }
        }
        return count;
    }
0
jaatgfg112 months ago
hi geeks -
ezz cpp soln --
        int getCount (string S, int N)       {           map<char,int>mp;           int n=S.length();           for(int i=0;i<n;i++){               if(S[i]!=S[i+1]){                  mp[S[i]]++;               }           }           map<char,int>::iterator it;           int ct=0;           for(it=mp.begin();it!=mp.end();it++){               if(it->second==N){                   ct++;               }                          }           return ct;       }
0
aarisettydivya2 months ago
Hey guys, the problem statement said that the  expected auxiliary space is O(1). Then how can we use array or any data Structure. I dnt get it. Can anyone explain it?
+1
codeman992 months ago
//c++
class Solution
{
  public:
        int getCount (string S, int N)
        {
            //code here.
            int cnt=0;
            string s="";
            for(int i=0;i<S.size()-1;i++){
                if(S[i]!=S[i+1]){
                    s+=S[i];
                }
            }
            s+=S[S.size()-1];
            S=s;
            unordered_map<char,int>mp;
            for(int i=0;i<S.size();i++){
                mp[S[i]]++;
            }
            for(auto x:mp){
                if(x.second==N)cnt++;
            }
            return cnt;
        }
};
0
virusnetic2 months ago
JAVA Solution
 
	int getCount (String S, int N) {
        
        HashMap<Character, Integer> map = new HashMap<>();
            
        Character prev = '#';
        for (Character c: S.toCharArray()) {
            if (prev != c)
                map.put(c, map.getOrDefault(c, 0) + 1);
            prev = c;
        }
        
        int count = 0;
        for (Character c: map.keySet())
            if (map.get(c) == N)
                count++;
        
        return count;
    }
0
akkeshri140420012 months ago
int getCount (string S, int N)       {           //code here.           int count=0;           unordered_map<char,int>m;           int n=S.length();           int i=0;           for(i=0;i<n-1;i++){               if(S[i]==S[i+1]){                   int j=i+1;                   m[S[i]]++;                   while(S[i]==S[j] and j<n){                       i++;                       j++;                   }               }               else{                          m[S[i]]++;               }                          }           if(i==n-1){               m[S[i]]++;           }                     for(auto x:m){               if(x.second==N){                   count++;               }           }           return count;                  }
0
mayank20213 months ago
c++int getCount (string S, int N)       {           unordered_map<char, int>newmap;           newmap[S[0]]++;           for(int i=0; i<S.size(); i++)            {             if(i>0 && S[i]!=S[i-1])             newmap[S[i]]++;            }           int count=0;           for(auto i:newmap)           {               if(i.second==N)                count++;           }             return count;       }
0
gbirla063 months ago
int getCount (string S, int N)       {           unordered_map<char,int>gb;           for(int i=0;i<S.length();i++)           {               if(i<(S.length()-1))               {                 if(S[i]!=S[i+1])                   gb[S[i]]++;               }               else                  gb[S[i]]++;           }           int count=0;           for(auto i=gb.begin();i!=gb.end();i++)           {               if(i->second==N)               count+=1;           }         return count;           }
+1
as0042303 months ago
In C++;
   {           int arr[26] = {0}, total = 0;                      for(int i = 0; i < S.size(); i++)           {               arr[S[i] - 'a']++;               if(S[i] == S[i+1])                 arr[S[i] - 'a']--;            }                      for(int i = 0; i < 26; i++)           {               if(arr[i] == N)                   total++;           }           return total;       }
0
chessnoobdj4 months ago
c++ using hash

int getCount (string S, int k)
        {
            int hash[26] = {0}, i=0, N = S.size(), cnt = 0;
            while(i < N){
                hash[S[i]-'a']++;
                while(i < N-1 && S[i] == S[i+1])
                    i++;
                i++;
            }
            for(int i=0; i<26; i++){
                if(hash[i] == k)
                    cnt++;
            }
            return cnt;
        }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.