A geek once visited a magical island where he found a special creature. He named it as Smurf. He noticed a very strange thing there. The smurfs resembled the primary colors of light. To make it more clear, the primary colors of light are Red(R), Green(G), and Blue(B). He talked to one of the smurfs. The smurfs came to know that he is a good programmer. The smurfs suggested a deal that they will ask him one question and if he is able to answer that question, they will allow the geek to take anything from the island.
The question is as follows:
The smurfs possess a very magical property. When two smurfs of different colors meet with other, they gets converted into a smurf of the third color. How many minimum number of smurfs will be remaining after this transformation? The question looked simple to geek. However, the smurfs put another constraint to make the geek think more. The two smurfs must be adjacent to each other  to make the transformation take place. There are n smurfs the colors of which are given in an array a[].
Example 1:
Input: n = 5
a = {'R' , 'G', 'B', 'R', 'B'}
Output: 1
Explaination: First 'R' and 'G' makes 'B'. 
Then 'B' and 'R' makes 'G' and that 'G' 
with 'B' at index 2 makes 'R', Now the 'R' 
and the last 'B' makes 'G'.
Example 2:
Input: n = 2
a = {'R', 'R'}
Output: 2
Explaination: There are two 'R' s. So 
they cannot change to anything else.
Your Task:
You do not need to read input or print anything. Your task is to complete the function findMin() which takes n and a as input parameters and retunrs the minimum number of smurfs existing at the end.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ n ≤ 1000

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Ad-hoc

Starter code: #User function Template for python3



class Solution:

    def minFind(self, n, a):

        # code here

Solutions:
class Solution:

	def minFind(self, n, a):
		r = a.count('R')
		b = a.count('B')
		g = a.count('G')
		if r == n or b == n or g == n:
			return n
		elif r % 2 == 0 and b % 2 == 0 and (g % 2 == 0) or (r % 2 != 0 and b % 2 != 0 and (g % 2 != 0)):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		r = a.count('R')
		g = a.count('G')
		b = a.count('B')
		if max(r, max(b, g)) == n:
			return n
		elif r % 2 == b % 2 and r % 2 == g % 2:
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		if n == 1:
			return 1
		R_count = B_count = G_count = 0
		for i in a:
			if i == 'R':
				R_count += 1
			if i == 'G':
				G_count += 1
			if i == 'B':
				B_count += 1
		if R_count == n or B_count == n or G_count == n:
			return n
		elif R_count & 1 == 0 and G_count & 1 == 0 and (B_count & 1 == 0):
			return 2
		elif R_count & 1 == 1 and G_count & 1 == 1 and (B_count & 1 == 1):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		rc = a.count('R')
		bc = a.count('B')
		gc = a.count('G')
		if len(set(a)) == 1:
			return len(a)
		elif (gc % 2 == 0) & (rc % 2 == 0) & (bc % 2 == 0):
			return 2
		elif (gc % 2 != 0) & (rc % 2 != 0) & (bc % 2 != 0):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		k = 0
		r = 0
		g = 0
		b = 0
		dict = {'R': 1, 'G': 2, 'B': 3}
		if n == 0:
			return 0
		for i in range(n):
			if a[i] == 'R':
				r += 1
			elif a[i] == 'G':
				g += 1
			else:
				b += 1
		if r == n or b == n or g == n:
			return n
		if r % 2 == g % 2 and g % 2 == b % 2:
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		dict = {'R': 0, 'B': 0, 'G': 0}
		for i in a:
			if i in dict:
				dict[i] += 1
		if len(set(a)) == 1:
			return len(a)
		elif dict['R'] % 2 == 0 and dict['B'] % 2 == 0 and (dict['G'] % 2 == 0) or (dict['R'] % 2 != 0 and dict['B'] % 2 != 0 and (dict['G'] % 2 != 0)):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		(r, g, b) = (0, 0, 0)
		for i in range(n):
			if a[i] == 'R':
				r += 1
			if a[i] == 'G':
				g += 1
			if a[i] == 'B':
				b += 1
		if r == 0 and b == 0 and (g > 0):
			return n
		if r == 0 and b > 0 and (g == 0):
			return n
		if r > 0 and b == 0 and (g == 0):
			return n
		if r % 2 == 0 and g % 2 == 0 and (b % 2 == 0):
			return 2
		if r % 2 != 0 and g % 2 != 0 and (b % 2 != 0):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		(r, b, g) = (0, 0, 0)
		for i in a:
			if i == 'R':
				r += 1
			if i == 'G':
				g += 1
			if i == 'B':
				b += 1
		if a.count('R') == n or a.count('G') == n or a.count('B') == n:
			return n
		elif r % 2 == 0 and b % 2 == 0 and (g % 2 == 0) or (r % 2 != 0 and b % 2 != 0 and (g % 2 != 0)):
			return 2
		return 1


class Solution:

	def rec(self, a, n):
		res = []
		k = 0
		clr = ['R', 'G', 'B']
		if a.count(a[0]) == len(a):
			res = a
			return len(a)
		else:
			i = n
			if i != 0:
				k += 1
				res.append(a[0])
			while i < len(a) - 1:
				if a[i] != a[i + 1]:
					for j in clr:
						if j != a[i] and j != a[i + 1]:
							res.append(j)
							i += 1
							k += 2
							break
				else:
					res.append(a[i])
					k += 1
				i += 1
			if k != len(a):
				res.append(a[len(a) - 1])
			return self.rec(res, 0)

	def minFind(self, n, a):
		(r, g, b) = (0, 0, 0)
		for i in range(n):
			if a[i] == 'R':
				r += 1
			if a[i] == 'G':
				g += 1
			if a[i] == 'B':
				b += 1
		if r == len(a) or g == len(a) or b == len(a):
			return max(r, g, b)
		if r % 2 == g % 2 and g % 2 == b % 2:
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		(r, g, b) = (0, 0, 0)
		for ele in a:
			if ele == 'R':
				r += 1
			elif ele == 'G':
				g += 1
			else:
				b += 1
		if r == n or g == n or b == n:
			return n
		if r % 2 == g % 2 and g % 2 == b % 2:
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		if len(set(a)) == 1:
			return len(a)
		x = a.count('R')
		y = a.count('G')
		z = a.count('B')
		if x % 2 == y % 2 == z % 2:
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		r = a.count('R')
		g = a.count('G')
		b = a.count('B')
		v = [r, g, b]
		if v.count(0) == 2:
			return n
		if all((i % 2 == 0 for i in v)):
			return 2
		if all((i % 2 for i in v)):
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		(red, green, blue) = (0, 0, 0)
		for smurf in a:
			if smurf == 'R':
				red += 1
			elif smurf == 'G':
				green += 1
			else:
				blue += 1
		if red == 0 and blue == 0 or (red == 0 and green == 0) or (green == 0 and blue == 0):
			return n
		elif red % 2 == 0 and blue % 2 == 0 and (green % 2 == 0) or (red % 2 != 0 and blue % 2 != 0 and (green % 2 != 0)):
			return 2
		else:
			return 1


class Solution:

	def get_color(self, c1, c2):
		if c1 == 'R':
			if c2 == 'G':
				return 'B'
			else:
				return 'G'
		elif c1 == 'G':
			if c2 == 'R':
				return 'B'
			else:
				return 'R'
		elif c1 == 'B':
			if c2 == 'G':
				return 'R'
			else:
				return 'G'

	def merge_smurfs(self, n, a):
		done = True
		result = []
		j = 0
		for i in range(1, n, 2):
			if a[i - 1] != a[i]:
				done = False
				result.append(self.get_color(a[i - 1], a[i]))
				j += 1
			else:
				result.append(a[i - 1])
				result.append(a[i])
		if n % 2 != 0:
			result.append(a[n - 1])
		if j > 0:
			a = result
		return done

	def rec(self, n, a):
		if n < 2:
			return a
		if n == 2:
			if a[0] != a[1]:
				print('merged {} size {} to {}'.format(a, n, self.get_color(a[0], a[1])))
				return [self.get_color(a[0], a[1])]
			return a
		mid = n // 2
		left = self.rec(mid, a[0:mid])
		right = self.rec(mid + n % 2, a[mid:n])
		return left + right

	def minFind(self, n, a):
		i = 0
		(r, g, b) = (0, 0, 0)
		while i < n:
			if a[i] == 'R':
				r += 1
			elif a[i] == 'G':
				g += 1
			elif a[i] == 'B':
				b += 1
			i += 1
		if r % 2 == 0 and g % 2 == 0 and (b % 2 == 0):
			return 2
		if r % 2 != 0 and g % 2 != 0 and (b % 2 != 0):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		d = {'R': 0, 'G': 0, 'B': 0}
		for i in a:
			d[i] += 1
		if d['R'] == 0 and d['G'] == 0 or (d['R'] == 0 and d['B'] == 0) or (d['B'] == 0 and d['G'] == 0):
			return n
		if d['R'] % 2 == 0 and d['G'] % 2 == 0 and (d['B'] % 2 == 0) or (d['R'] % 2 != 0 and d['G'] % 2 != 0 and (d['B'] % 2 != 0)):
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		i = 0
		(r, g, b) = (0, 0, 0)
		while i < n:
			if a[i] == 'R':
				r += 1
			elif a[i] == 'G':
				g += 1
			elif a[i] == 'B':
				b += 1
			i += 1
		if r % 2 == 0 and g % 2 == 0 and (b % 2 == 0):
			return 2
		if r % 2 != 0 and g % 2 != 0 and (b % 2 != 0):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		arr = []
		for i in a:
			if i == 'R':
				arr.append(1)
			elif i == 'G':
				arr.append(2)
			else:
				arr.append(3)
		res = 0
		for i in arr:
			res = res ^ i
		if res == 0:
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		(R, B, G) = (0, 0, 0)
		for i in a:
			if i == 'R':
				R += 1
			elif i == 'G':
				G += 1
			else:
				B += 1
		if R & 1 and G & 1 and B & 1 or (R % 2 == 0 and G % 2 == 0 and (B % 2 == 0)):
			return 2
		elif R and (not G) and (not B):
			return R
		elif G and (not R) and (not B):
			return G
		elif B and (not G) and (not R):
			return B
		return 1


class Solution:

	def minFind(self, n, a):
		if a.count('R') % 2 == 0 and a.count('G') % 2 == 0 and (a.count('B') % 2 == 0) or (a.count('R') % 2 == 1 and a.count('G') % 2 == 1 and (a.count('B') % 2 == 1)):
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		r = a.count('R')
		g = a.count('G')
		b = a.count('B')
		v = 0
		f = [r, g, b].count('0')
		if f == 2:
			return max(r, g, b)
		if r % 2:
			v += 1
		if g % 2:
			v += 1
		if b % 2:
			v += 1
		if v == 3 or v == 0:
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		if n <= 2:
			return n
		dic = {}
		oc = 0
		ec = 0
		for i in a:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		for (i, j) in dic.items():
			if j % 2 == 0:
				ec += 1
			else:
				oc += 1
		if ec == 3 or ec == 0:
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		from collections import Counter
		smurf = list(Counter(a).values())
		if len(smurf) == 1:
			return n
		if all((s & 1 == 0 for s in smurf)) or (len(smurf) == 3 and all((s & 1 == 1 for s in smurf))):
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		d = {}
		e = 0
		o = 0
		for i in a:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		l = list(d.values())
		for i in l:
			if i == n:
				return n
			elif i % 2 == 0:
				e += 1
			else:
				o += 1
		if e != 0 and o == 0:
			return 2
		elif e == 0 and o != 0:
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		r = ['R', 'B', 'G']
		(c, d, e) = (0, 0, 0)
		for i in a:
			if i == 'R':
				c += 1
			elif i == 'B':
				d += 1
			else:
				e += 1
		if c == n or d == n or e == n:
			return n
		elif c % 2 == 0 and d % 2 == 0 and (e % 2 == 0):
			return 2
		elif c % 2 != 0 and d % 2 != 0 and (e % 2 != 0):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		rc = 0
		bc = 0
		gc = 0
		for i in a:
			if i == 'R':
				rc += 1
			elif i == 'B':
				bc += 1
			else:
				gc += 1
		if rc % 2 == 0 and bc % 2 == 0 and (gc % 2 == 0) or (rc % 2 != 0 and bc % 2 != 0 and (gc % 2 != 0)):
			return 2
		elif rc == n or bc == n or gc == n:
			return n
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		redc = 0
		bluec = 0
		greenc = 0
		for i in range(n):
			if a[i] == 'R':
				redc += 1
			elif a[i] == 'G':
				bluec += 1
			else:
				greenc += 1
		if redc == 0 and bluec == 0 or (redc == 0 and greenc == 0) or (greenc == 0 and bluec == 0):
			return n
		elif redc % 2 == 0 and bluec % 2 == 0 and (greenc % 2 == 0) or (redc % 2 == 1 and bluec % 2 == 1 and (greenc % 2 == 1)):
			return 2
		else:
			return 1


class Solution:

	def minFind(self, n, a):
		y = [0] * 3
		for r in range(n):
			if a[r] == 'R':
				y[0] += 1
			elif a[r] == 'G':
				y[1] += 1
			else:
				y[2] += 1
		if n in y:
			return n
		if y[0] % 2 == y[1] % 2 and y[1] % 2 == y[2] % 2:
			return 2
		return 1


from collections import defaultdict

class Solution:

	def minFind(self, n, a):
		(r, g, b) = (0, 0, 0)
		for i in range(n):
			if a[i] == 'R':
				r += 1
			if a[i] == 'G':
				g += 1
			if a[i] == 'B':
				b += 1
		if r % 2 == g % 2 and g % 2 == b % 2:
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		cntr = cntg = cntb = 0
		for i in range(n):
			if a[i] == 'R':
				cntr += 1
			elif a[i] == 'B':
				cntg += 1
			else:
				cntb += 1
		if cntr == n or cntg == n or cntb == n:
			return n
		elif cntr & 1 == 0 and cntg & 1 == 0 and (cntb & 1 == 0):
			return 2
		elif cntr & 1 != 0 and cntg & 1 != 0 and (cntb & 1 != 0):
			return 2
		else:
			return 1


from collections import defaultdict

class Solution:

	def minFind(self, n, a):
		r = a.count('R')
		b = a.count('B')
		g = a.count('G')
		if r % 2 == 0 and g % 2 == 0 and (b % 2 == 0):
			return 2
		if r % 2 != 0 and g % 2 != 0 and (b % 2 != 0):
			return 2
		else:
			return 1
		pass


class Solution:

	def minFind(self, n, ar):
		(a, b, c) = (0, 0, 0)
		for i in range(n):
			if ar[i] == 'R':
				a += 1
			if ar[i] == 'G':
				b += 1
			if ar[i] == 'B':
				c += 1
		if a % 2 == b % 2 and b % 2 == c % 2:
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		(red, grn, blu) = (0, 0, 0)
		for i in range(n):
			if a[i] == 'R':
				red += 1
			if a[i] == 'G':
				grn += 1
			if a[i] == 'B':
				blu += 1
		if red % 2 == grn % 2 and grn % 2 == blu % 2:
			return 2
		return 1


class Solution:

	def minFind(self, n, a):
		countR = 0
		countB = 0
		countG = 0
		for i in range(n):
			if a[i] == 'R':
				countR += 1
			elif a[i] == 'G':
				countG += 1
			else:
				countB += 1
		if countR == n or countG == n or countB == n:
			return n
		elif countR % 2 == 0 and countG % 2 == 0 and (countB % 2 == 0):
			return 2
		elif countR % 2 == 1 and countG % 2 == 1 and (countB % 2 == 1):
			return 2
		else:
			return 1