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.
Ã¢â¬â¹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|<=10^{5}
1<=N<=10^{3}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def getCount (self,S, N):
        # your code here

Solutions:
class Solution:

	def getCount(self, S, N):
		i = 1
		while i < len(S) - 1:
			if S[i] == S[i - 1]:
				S = S[:i] + S[i + 1:]
				i -= 1
			if S[i] == S[i + 1]:
				S = S[:i + 1] + S[i + 2:]
				i -= 1
			i += 1
		my_dict = {}
		for i in range(len(S)):
			if S[i] in my_dict:
				my_dict[S[i]] += 1
			else:
				my_dict[S[i]] = 1
		count = 0
		for (x, y) in my_dict.items():
			if y == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, n):
		count = {}
		for i in range(len(S)):
			if i == 0 or S[i] != S[i - 1]:
				count[S[i]] = count.get(S[i], 0) + 1
		return sum((1 for c in count.values() if c == n))


class Solution:

	def getCount(self, S, N):
		d = {}
		store = ''
		count = 0
		for i in S:
			if i not in d:
				d[i] = 1
			elif store != i:
				d[i] += 1
			store = i
		for key in d.keys():
			if d[key] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		count = {}
		i = 0
		res = 0
		l = len(S)
		for i in range(l):
			if i == 0:
				count[S[i]] = 1
			elif S[i] == S[i - 1]:
				continue
			elif S[i] not in count:
				count[S[i]] = 1
			else:
				count[S[i]] += 1
		for value in count.values():
			if value == N:
				res += 1
		return res


from collections import Counter

class Solution:

	def getCount(self, S, N):
		items = S[0]
		for char in S:
			if char != items[-1]:
				items += char
		return sum([value == N for value in Counter(items).values()])


class Solution:

	def getCount(self, S, N):
		d = {}
		count = 0
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				d[S[i]] = d.get(S[i], 0) + 1
		d[S[-1]] = d.get(S[-1], 0) + 1
		for value in d.values():
			if value == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		res = S[0]
		count = 0
		mp = {}
		for i in range(1, len(S)):
			if S[i] == S[i - 1]:
				continue
			res += S[i]
		for i in res:
			if i in mp:
				mp[i] += 1
			else:
				mp[i] = 1
		for i in mp:
			if mp[i] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		dic = {}
		dic[S[0]] = 1
		prev = S[0]
		for i in S:
			if i in dic:
				if prev != i:
					dic[i] += 1
					prev = i
			else:
				prev = i
				dic[i] = 1
		an = 0
		for i in dic:
			if dic[i] == N:
				an += 1
		return an


from collections import Counter

class Solution:

	def getCount(self, S, N):
		d = {}
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				d[S[i]] = d.get(S[i], 0) + 1
		d[S[-1]] = d.get(S[-1], 0) + 1
		count = 0
		for value in d.values():
			if value == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		s1 = ' '
		for i in S:
			if i != s1[-1]:
				s1 += i
		d = {}
		for i in s1[1:]:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		c = 0
		for (x, y) in d.items():
			if y == N:
				c += 1
		return c


from collections import Counter

class Solution:

	def getCount(self, S, N):
		s = S
		l = len(s)
		d = {}
		c = 0
		for i in range(l):
			if i != 0 and s[i] not in d and (s[i - 1] != s[i]):
				d[s[i]] = 1
			elif i == 0 and s[i] not in d:
				d[s[i]] = 1
			elif i != 0 and s[i] in d and (s[i - 1] != s[i]):
				d[s[i]] += 1
		for i in d.keys():
			if d[i] == N:
				c = c + 1
		return c


class Solution:

	def getCount(self, S, N):
		d = dict()
		c = 0
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				d[S[i]] = d.get(S[i], 0) + 1
		d[S[-1]] = d.get(S[-1], 0) + 1
		for i in d.values():
			if i == N:
				c += 1
		return c


class Solution:

	def getCount(self, S, N):
		d = {}
		d[S[0]] = 1
		for i in range(1, len(S)):
			if S[i] == S[i - 1]:
				continue
			else:
				if S[i] not in d:
					d[S[i]] = 0
				d[S[i]] += 1
		c = 0
		for i in d:
			if d[i] == N:
				c += 1
		return c


from collections import defaultdict

class Solution:

	def getCount(self, S, N):
		d = defaultdict(int)
		prev = S[0]
		d[S[0]] += 1
		for i in range(1, len(S)):
			if S[i] != prev:
				d[S[i]] += 1
				prev = S[i]
		ans = 0
		for i in d:
			if d[i] == N:
				ans += 1
		return ans


class Solution:

	def getCount(self, S, N):
		last = 's'
		table = {}
		for i in S:
			if i in table:
				if i != last:
					table[i] += 1
				last = i
			else:
				table[i] = 1
				last = i
		count = 0
		for i in table:
			if table[i] == N:
				count += 1
		return count


class Solution:

	def getCount(self, s, N):
		count_dict = {}
		for i in range(len(s)):
			if i == 0 or s[i] != s[i - 1]:
				count_dict[s[i]] = count_dict.get(s[i], 0) + 1
		count = 0
		for c in count_dict:
			if count_dict[c] == n:
				count += 1
		return count


class Solution:

	def getCount(self, s, n):
		times = {}
		total = 0
		i = 0
		while i < len(s) - 1:
			if s[i] == s[i + 1]:
				i += 1
			else:
				if s[i] not in times:
					times[s[i]] = 1
				else:
					times[s[i]] += 1
				i += 1
		if s[-1] in times:
			times[s[-1]] += 1
		else:
			times[s[-1]] = 1
		for k in times:
			if times[k] == n:
				total += 1
		return total


class Solution:

	def getCount(self, S, N):
		dic = {}
		count = 0
		k = S[0]
		for i in range(1, len(S)):
			if k[-1] == S[i]:
				k = S[i]
			else:
				if k not in dic:
					dic[k] = 1
				else:
					dic[k] += 1
				k = S[i]
		if k not in dic:
			dic[k] = 1
		else:
			dic[k] += 1
		for i in dic:
			if dic[i] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		from collections import Counter
		import string
		c = ' '
		s.replace(' ', '')
		for i in range(0, len(s)):
			if c[-1] != s[i] and s[i] in string.ascii_letters:
				c = c + s[i]
		c = c.strip()
		c = Counter(c)
		count = 0
		for i in c:
			if c[i] == N:
				count = count + 1
		return count


class Solution:

	def getCount(self, S, N):
		dict1 = {}
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				dict1[S[i]] = dict1.get(S[i], 0) + 1
		dict1[S[-1]] = dict1.get(S[-1], 0) + 1
		c = 0
		for j in dict1.values():
			if j == N:
				c += 1
		return c


from collections import Counter

class Solution:

	def getCount(self, S, N):
		dict1 = {}
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				dict1[S[i]] = dict1.get(S[i], 0) + 1
		dict1[S[-1]] = dict1.get(S[-1], 0) + 1
		c = 0
		for j in dict1.values():
			if j == N:
				c += 1
		return c


class Solution:

	def getCount(self, S, N):
		count = 0
		hash = [0] * 26
		index = ord(s[0]) - ord('a')
		hash[index] += 1
		for i in range(1, len(S)):
			if S[i] != S[i - 1]:
				index = ord(s[i]) - ord('a')
				hash[index] += 1
		for i in range(0, len(hash)):
			if hash[i] == N:
				count += 1
		return count


from collections import Counter

class Solution:

	def getCount(self, S, N):
		val = S[0]
		for i in S:
			if i != val[-1]:
				val += i
		return sum([val1 == N for val1 in Counter(val).values()])


class Solution:

	def remove_consecutively_occuerence(self, s):
		new_s = ''
		for i in range(len(s) - 1):
			if s[i] == s[i + 1]:
				continue
			else:
				new_s += s[i]
		if s[-2] == s[-1]:
			return new_s
		return new_s + s[-1]

	def getCount(self, S, N):
		S = self.remove_consecutively_occuerence(S)
		freq = {}
		for i in S:
			try:
				freq[i] = freq.get(i, 0) + 1
			except:
				freq[i] = 1
		count = 0
		for i in freq.values():
			if i == N:
				count += 1
		return count


from collections import Counter

class Solution:

	def getCount(self, S, N):
		ans = 0
		hm = dict()
		for (idx, s) in enumerate(S):
			if s not in hm:
				hm[s] = 1
				continue
			if S[idx - 1] != s:
				hm[s] += 1
		for (k, v) in hm.items():
			if v == N:
				ans += 1
		return ans


class Solution:

	def getCount(self, S, N):
		count = 0
		s = ''
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				s += S[i]
		s += S[-1]
		S = s
		track = {}
		for i in range(len(S)):
			if S[i] not in track:
				track[S[i]] = 1
			else:
				track[S[i]] += 1
		for e in track:
			if track[e] == N:
				count += 1
		return count


from collections import Counter
from itertools import groupby

class Solution:

	def getCount(self, S, N):
		c = 0
		a = list(groupby(S))
		p = ''
		for (i, j) in a:
			p += i
		t = Counter(p)
		for (i, j) in t.items():
			if j == N:
				c += 1
		return c


class Solution:

	def getCount(self, S, n):
		p = {}
		t = ''
		for i in s:
			if i not in p:
				p[i] = 1
			elif t != i:
				p[i] += 1
			t = i
		count = 0
		for key in p.keys():
			if p[key] == n:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		char_dict = {}
		last_char = ''
		for char in S:
			if char == last_char:
				last_char = char
				continue
			if char in char_dict:
				char_dict[char] = char_dict[char] + 1
			else:
				char_dict[char] = 1
			last_char = char
		char_count = 0
		for ele in char_dict:
			if char_dict[ele] == N:
				char_count = char_count + 1
		return char_count


class Solution:

	def getCount(self, s, n):
		d = {}
		d[s[0]] = 1
		for i in range(1, len(s)):
			if s[i] in d:
				if s[i] != s[i - 1]:
					d[s[i]] += 1
			else:
				d[s[i]] = 1
		c = 0
		for i in d:
			if d[i] == n:
				c += 1
		return c


class Solution:

	def getCount(self, S, N):
		dic = {}
		l = len(S) - 1
		for i in range(l):
			if S[i] != S[i + 1]:
				dic[S[i]] = dic.get(S[i], 0) + 1
		ans = 0
		dic[S[l]] = dic.get(S[l], 0) + 1
		for value in dic.values():
			if value == N:
				ans = ans + 1
		return ans


class Solution:

	def getCount(self, S, N):
		s = ''
		s = s + S[0]
		c = 0
		for i in range(1, len(S)):
			if S[i] != s[-1]:
				s = s + S[i]
		from collections import Counter
		d = Counter(s)
		for (i, j) in d.items():
			if j == N:
				c = c + 1
		return c


class Solution:

	def getCount(self, S, N):
		ls = list(S)
		dict = {}
		count = 0
		prv = ''
		for e in ls:
			if e == prv:
				prv = e
				continue
			try:
				dict[e] = dict[e] + 1
			except:
				dict[e] = 1
			prv = e
		for (i, e) in dict.items():
			if e == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		freq = [0 for i in range(26)]
		n = len(S)
		i = 0
		while i < n:
			j = i
			while j < n and S[j] == S[i]:
				j += 1
			freq[ord(S[i]) - ord('a')] += 1
			i = j
		res = 0
		for i in range(26):
			if freq[i] == N:
				res += 1
		return res


class Solution:

	def getCount(self, S, N):
		n = [0] * 26
		c = 0
		for i in range(len(S) - 1):
			x = ord(S[i])
			if s[i] != s[i + 1]:
				n[x - 97] += 1
			else:
				i += 1
		n[ord(S[-1]) - 97] += 1
		for j in range(len(n)):
			if n[j] == N:
				c += 1
		return c


class Solution:

	def getCount(self, S, N):
		mapp = [0] * 26
		temp = S[0]
		mapp[ord(temp) - 97] += 1
		for i in S:
			if temp != i:
				temp = i
				mapp[ord(temp) - 97] += 1
		count = 0
		for x in mapp:
			if x == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		d = dict()
		count = 0
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				d[S[i]] = d.get(S[i], 0) + 1
		d[S[-1]] = d.get(S[-1], 0) + 1
		for i in d.keys():
			if d[i] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		freq = {}
		count = 0
		track = ''
		for i in S:
			if i not in freq:
				freq[i] = 1
			elif track != i:
				freq[i] += 1
			track = i
		for key in freq.keys():
			if freq[key] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):

		def hash(s):
			hash = {}
			for i in range(len(s)):
				if i != len(s) - 1 and s[i] == s[i + 1]:
					continue
				if s[i] in hash.keys():
					hash[s[i]] += '1'
				else:
					hash.update({s[i]: '1'})
			return hash
		count = 0
		hash = hash(S)
		for v in hash.values():
			if len(v) == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		res = S[0]
		for i in S[1:]:
			if i != res[-1]:
				res += i
		count = 0
		for i in set(res):
			if res.count(i) == N:
				count += 1
		return count


class Solution:

	def getCount(self, s, n):
		d1 = [0] * 256
		d1[ord(s[0])] = 1
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				pass
			else:
				d1[ord(s[i])] += 1
		cnt = 0
		for i in range(len(d1)):
			if d1[i] == n:
				cnt += 1
		return cnt


class Solution:

	def getCount(self, S, N):
		occ = {}
		count = 0
		for i in range(0, len(S)):
			if occ.get(S[i]) == None:
				occ[S[i]] = 1
			elif S[i - 1] == s[i]:
				continue
			else:
				occ[s[i]] += 1
		for ch in occ:
			if occ[ch] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		h = {}
		track = ''
		count = 0
		for ch in S:
			if ch not in h:
				h[ch] = 1
			elif track != ch:
				h[ch] += 1
			track = ch
		for key in h.keys():
			if h[key] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		data = {}
		for i in range(len(S)):
			data[S[i]] = 0
		prv = ''
		for i in range(len(S)):
			if prv != S[i]:
				data[S[i]] += 1
			prv = S[i]
		count = 0
		for ele in data:
			if data[ele] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		hashmap = {S[0]: 1}
		count = 0
		for i in range(1, len(S)):
			if S[i] != S[i - 1]:
				if S[i] not in hashmap:
					hashmap[S[i]] = 1
				else:
					hashmap[S[i]] += 1
		for i in hashmap.values():
			if i == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		H = {}
		count = 0
		for i in range(len(S)):
			if S[i] in H.keys():
				temp = H[S[i]]
			H[S[i]] = H.get(S[i], 0) + 1
			if i > 0 and S[i] == S[i - 1]:
				H[S[i]] = temp
		for key in H.keys():
			if H[key] == N:
				count += 1
		return count


from collections import Counter

class Solution:

	def getCount(self, S, N):
		h = {}
		track = ''
		count = 0
		for ch in S:
			if ch not in h:
				h[ch] = 1
			elif track != ch:
				h[ch] += 1
			track = ch
		for key in h.keys():
			if h[key] == N:
				count += 1
		return count


from collections import Counter

class Solution:

	def getCount(self, S, N):
		s = S
		n = N
		a = [s[0]]
		for i in s:
			if i != a[-1]:
				a.append(i)
		counter = Counter(a)
		count = 0
		for i in counter.values():
			if i == n:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		x = S[0]
		prev = x
		for i in range(1, len(S)):
			if S[i] == prev:
				continue
			x += S[i]
			prev = S[i]
		S = tuple(set(x))
		return sum((x.count(i) == n for i in S))


class Solution:

	def getCount(self, S, N):
		s = S
		cou = ind = res = 0
		mp = {}
		for i in s:
			if i not in mp:
				mp[i] = 1
			elif i != s[ind - 1]:
				mp[i] += 1
			ind += 1
		for i in mp:
			if mp[i] == N:
				res += 1
		return res


class Solution:

	def getCount(self, S, N):
		from collections import Counter as C
		a = [S[0]]
		for x in S:
			if x != a[-1]:
				a.append(x)
		d = C(a)
		count = 0
		for x in d.values():
			if x == N:
				count += 1
		return count


from collections import Counter

class Solution:

	def getCount(self, S, N):
		occurences = [S[0]]
		for i in range(1, len(S)):
			if S[i] != S[i - 1]:
				occurences.append(S[i])
		c = Counter(occurences)
		return sum([value == N for value in c.values()])


class Solution:

	def getCount(self, S, N):
		k = []
		for i in S:
			if k == []:
				k.append(i)
			elif k[-1] != i:
				k.append(i)
		r = ''.join(map(str, k))
		d = dict()
		for i in r:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		c = 0
		for i in d:
			if d[i] == N:
				c += 1
		return c


class Solution:

	def getCount(self, S, N):
		d = {}
		j = ''
		for i in s:
			if j == i:
				d[i] -= 1
			if i not in d:
				d[i] = 1
				j = i
			else:
				d[i] += 1
				j = i
		ans = 0
		for i in d.values():
			if i == N:
				ans += 1
		return ans


class Solution:

	def getCount(self, S, N):
		d = {}
		string = ''
		string2 = ''
		string1 = ''
		string3 = ''
		for i in range(len(S) - 1):
			if S[i] != S[i + 1]:
				string += S[i]
		S = string + S[len(S) - 1]
		for i in S:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in S:
			if i not in string1:
				string2 += i
		for i in d:
			if d[i] == N:
				string1 += i
		for i in string1:
			if i not in string3:
				string3 += i
		return len(string3)


class Solution:

	def getCount(self, S, N):
		stack_top = ''
		result = ''
		final = ''
		for i in S:
			if i != stack_top:
				result += i
				stack_top = i
		for i in set(result):
			if result.count(i) == N:
				final += i
		return len(final)


class Solution:

	def getCount(self, S, N):
		d = {}
		count = 0
		for i in range(len(S)):
			if S[i] in d:
				if S[i] != S[i - 1]:
					d[S[i]] += 1
			else:
				d[S[i]] = 1
		for i in d:
			if d[i] == N:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		dic = dict()
		for i in range(len(S)):
			if i == 0:
				dic[S[i]] = 1
			elif S[i] == S[i - 1]:
				continue
			elif S[i] not in dic:
				dic[S[i]] = 1
			else:
				dic[S[i]] += 1
		c = 0
		for i in dic.values():
			if i == N:
				c += 1
		return c


class Solution:

	def getCount(self, s, k):
		i = 1
		prev = s[0]
		n = len(s)
		from collections import defaultdict
		d = defaultdict(int)
		while i < n:
			if s[i] != prev:
				d[prev] += 1
				prev = s[i]
			i += 1
		d[prev] += 1
		count = 0
		for i in d.keys():
			if d[i] == k:
				count += 1
		return count


class Solution:

	def getCount(self, S, N):
		A = {}
		for c in S:
			if c not in A:
				A[c] = 0
			A[c] += 1
		for i in range(len(S) - 1):
			if S[i] == S[i + 1]:
				A[S[i]] -= 1
		cnt = 0
		for k in A:
			if A[k] == N:
				cnt += 1
		return cnt


class Solution:

	def getCount(self, S, N):
		d = {}
		c = 0
		n = len(S)
		for i in range(n):
			if i == 0:
				d[S[i]] = 1
			elif S[i] == S[i - 1]:
				continue
			elif S[i] in d:
				d[S[i]] += 1
			else:
				d[S[i]] = 1
		for value in d.values():
			if value == N:
				c += 1
		return c


class Solution:

	def getCount(self, S, N):
		new_s = s[0]
		new = []
		freq = {}
		for i in range(0, len(s) - 1):
			if s[i] != s[i + 1]:
				new_s = new_s + s[i + 1]
		for char in new_s:
			if char in freq:
				freq[char] += 1
			else:
				freq[char] = 1
		for letter in freq:
			if freq[letter] == N:
				new.append(letter)
		return len(new)


from collections import defaultdict

class Solution:

	def getCount(self, S, N):
		l = []
		for letter in S:
			if len(l) != 0:
				if l[-1] != letter:
					l.append(letter)
			else:
				l.append(letter)
		S = ''.join(l)
		count = defaultdict(lambda : 0)
		for letter in S:
			count[letter] += 1
		total = 0
		for key in count.keys():
			if count[key] == N:
				total += 1
		del count
		return total