Given a string of length N, made up of only uppercase characters 'R' and 'G', where 'R' stands for Red and 'G' stands for Green.Find out the minimum number of characters you need to change to make the whole string of the same colour.
Example 1:
Input:
N=5
S="RGRGR"
Output:
2
Explanation:
We need to change only the 2nd and 
4th(1-index based) characters to 'R', 
so that the whole string becomes 
the same colour.
Example 2:
Input:
N=7
S="GGGGGGR"
Output:
1
Explanation:
We need to change only the last 
character to 'G' to make the string 
same-coloured.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function RedOrGreen() which takes the Integer N and the string S as input parameters and returns the minimum number of characters that have to be swapped to make the string same-coloured.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1<=N<=10^{5}
S consists only of characters 'R' and 'G'.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

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

Solutions:
class Solution:

	def RedOrGreen(self, N, S):
		d = {}
		for i in S:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		k = min(d.values())
		if k == N:
			return 0
		return k


class Solution:

	def RedOrGreen(self, N, S):
		green = 0
		red = 0
		for i in range(N):
			if S[i] == 'G':
				green = green + 1
			else:
				red = red + 1
		if red > green:
			return N - red
		else:
			return N - green


class Solution:

	def RedOrGreen(self, N, S):
		RedCount = 0
		GreenCount = 0
		for i in range(len(S)):
			if S[i] == 'R':
				RedCount += 1
			else:
				GreenCount += 1
		return min(RedCount, GreenCount)


class Solution:

	def RedOrGreen(self, N, S):
		return min(S.count('R'), S.count('G'))


class Solution:

	def RedOrGreen(self, n, s):
		c = 0
		d = 0
		for i in s:
			if i == 'R':
				c += 1
			else:
				d += 1
		return min(c, d)


from collections import Counter

class Solution:

	def RedOrGreen(self, N, S):
		set_ = set(S)
		if len(set_) == 1:
			return 0
		else:
			dict_ = dict(Counter(S))
			lst_1 = []
			for value in dict_.values():
				if value not in lst_1:
					lst_1.append(value)
			if len(lst_1) == 1:
				return N // 2
			else:
				max_Value = max(dict_.values())
				lst = []
				for (key, value) in dict_.items():
					if value >= max_Value:
						lst.append(key)
				lst_ = []
				for i in S:
					if i not in lst:
						lst_.append(i)
				result = len(lst_)
				return result


class Solution:

	def RedOrGreen(self, N, S):
		r = 0
		g = 0
		for i in range(0, len(S)):
			if S[i] == 'R':
				r = r + 1
			else:
				g = g + 1
		if g > r:
			return r
		else:
			return g


class Solution:

	def RedOrGreen(self, N, S):
		c = 0
		d = 0
		for i in range(N):
			if S[i] == 'R':
				c += 1
			else:
				d += 1
		if c < d:
			return c
		else:
			return d


class Solution:

	def RedOrGreen(self, N, S):
		r = 0
		g = 0
		for i in S:
			if i == 'R':
				r = r + 1
			elif i == 'G':
				g = g + 1
		if r > g:
			v = g
		else:
			v = r
		return v


class Solution:

	def RedOrGreen(self, N, S):
		red = 0
		green = 0
		for i in S:
			if i == 'R':
				red += 1
			elif i == 'G':
				green += 1
		if red < green:
			ans = red
		else:
			ans = green
		return ans


class Solution:

	def RedOrGreen(self, N, S):
		green = S.count('G')
		red = S.count('R')
		if green > red:
			return red
		elif green < red:
			return green
		else:
			return green


class Solution:

	def RedOrGreen(self, N, S):
		r = S.count('R')
		g = S.count('G')
		return N - max(r, g)


class Solution:

	def RedOrGreen(self, N, S):
		r = 0
		g = 0
		for i in S:
			if i == 'R':
				r += 1
			if i == 'G':
				g += 1
		return min(r, g)


class Solution:

	def RedOrGreen(self, N, S):
		l = []
		k = []
		for i in S:
			if i == 'R':
				l.append(i)
			else:
				k.append(i)
		if len(l) > len(k):
			return len(k)
		else:
			return len(l)


class Solution:

	def RedOrGreen(self, N, S):
		c1 = 0
		c2 = 0
		for i in S:
			if i == 'R':
				c1 += 1
			else:
				c2 += 1
		return min(c1, c2)


class Solution:

	def RedOrGreen(self, N, S):
		red = 0
		for i in S:
			if i == 'R':
				red += 1
		return min(red, N - red)


class Solution:

	def RedOrGreen(self, N, S):
		a = S.count('R')
		b = S.count('G')
		if a < b:
			return N - b
		return N - a


class Solution:

	def RedOrGreen(self, N, S):
		p = S.count('R')
		k = S.count('G')
		if p > k:
			return k
		return p


class Solution:

	def RedOrGreen(self, N, S):
		a = ''
		b = ''
		for i in S:
			if i == 'R':
				a += i
			if i == 'G':
				b += i
		if len(b) > len(a):
			c = len(a)
		else:
			c = len(b)
		return c


class Solution:

	def RedOrGreen(self, N, S):
		k = 0
		n = 0
		for i in S:
			if i == 'R':
				k += 1
			else:
				n += 1
		if n > k:
			return k
		else:
			return n


class Solution:

	def RedOrGreen(self, N, S):
		l = list(S)
		x = S.count('R')
		y = S.count('G')
		if x < y:
			return x
		else:
			return y


class Solution:

	def RedOrGreen(self, N, S):
		s = S
		n = N
		r = s.count('R')
		g = s.count('G')
		if r > g:
			return g
		else:
			return r


class Solution:

	def RedOrGreen(self, N, S):
		r = S.count('R')
		g = S.count('G')
		cnt = 0
		for i in range(N):
			if r > g and S[i] == 'G':
				cnt = g
			if r < g and S[i] == 'R':
				cnt = r
			if r == g:
				cnt = r
		return cnt


class Solution:

	def RedOrGreen(self, N, S):
		green = 0
		red = 0
		ch = ''
		for i in S:
			if i == 'R':
				green += 1
			else:
				red += 1
		if green > red:
			return red
		return green


class Solution:

	def RedOrGreen(self, N, S):
		count = 0
		count1 = 0
		for i in range(N):
			if S[i] == 'R':
				count += 1
				continue
		for j in range(N):
			if S[j] == 'G':
				count1 += 1
				continue
		if count > count1:
			return count1
		else:
			return count


class Solution:

	def RedOrGreen(self, N, S):
		c_r = 0
		c_g = 0
		for i in S:
			if i == 'R':
				c_r += 1
			else:
				c_g += 1
		if c_g > c_r:
			return c_r
		else:
			return c_g


class Solution:

	def RedOrGreen(self, N, S):
		x = S.count('G')
		y = N - x
		return min(x, y)


class Solution:

	def RedOrGreen(self, N, S):
		l = list(S)
		g = l.count('G')
		r = l.count('R')
		return min(r, g)


class Solution:

	def RedOrGreen(self, N, S):
		(green, red) = (0, 0)
		for letter in S:
			if letter == 'G':
				green += 1
			elif letter == 'R':
				red += 1
		return min(red, green)


class Solution:

	def RedOrGreen(self, N, S):
		d = list(S)
		j = d.count('R')
		m = d.count('G')
		n = [j, m]
		return min(n)


class Solution:

	def RedOrGreen(self, N, S):
		r = S.count('R')
		g = S.count('G')
		if r > g:
			return len(S) - r
		else:
			return len(S) - g


class Solution:

	def RedOrGreen(self, N, S):
		a = 0
		for i in S:
			if i == 'R':
				a = a + 1
		if N - a > a:
			return a
		else:
			return N - a


class Solution:

	def RedOrGreen(self, N, S):
		a = S.count('G')
		b = S.count('R')
		for i in range(N):
			if a >= b:
				return b
			elif b > a:
				return a


class Solution:

	def RedOrGreen(self, N, S):
		R_C = 0
		G_C = 0
		for i in S:
			if i == 'R':
				R_C += 1
			else:
				G_C += 1
		return min([R_C, G_C])


class Solution:

	def RedOrGreen(self, N, S):
		z = 0
		x = 0
		z = S.count('G')
		x = S.count('R')
		if z > x:
			return x
		else:
			return z


class Solution:

	def RedOrGreen(self, N, S):
		r = ''
		g = ''
		for i in S:
			if i == 'R':
				r += i
		for j in S:
			if j == 'G':
				g += j
		if len(r) > len(g):
			return len(g)
		else:
			return len(r)


class Solution:

	def RedOrGreen(self, N, S):
		count_red = 0
		count_green = 0
		for element in S:
			if element == 'R':
				count_red += 1
			elif element == 'G':
				count_green += 1
		if count_red < count_green:
			return count_red
		else:
			return count_green


class Solution:

	def RedOrGreen(self, N, S):
		s1 = S.count('R')
		s2 = S.count('G')
		if s1 > s2:
			count = s2
		else:
			count = s1
		return count


class Solution:

	def RedOrGreen(self, N, S):
		if len(set(S)) == 1:
			return 0
		ll = [(S.count(x), x) for x in set(S)]
		ll.sort()
		a = []
		for (i, x) in enumerate(ll):
			if i + 1 == len(ll):
				next_ = i
			else:
				next_ = i + 1
			S = S.replace(ll[i][1], ll[next_][1])
			if len(set(S)) == 1:
				a.append(x[0])
				break
		return sum(a)


class Solution:

	def RedOrGreen(self, N, S):
		count_red = 0
		count_green = 0
		for i in S:
			if i == 'R':
				count_red += 1
			else:
				count_green += 1
		if count_red > count_green:
			return count_green
		else:
			return count_red


class Solution:

	def RedOrGreen(self, N, S):
		p1 = S.count('G')
		p2 = S.count('R')
		if p1 < p2:
			return p1
		else:
			return p2


class Solution:

	def RedOrGreen(self, N, S):
		g_count = 0
		r_count = 0
		for i in S:
			if i == 'G':
				g_count += 1
			else:
				r_count += 1
		if g_count > r_count:
			return r_count
		else:
			return g_count


class Solution:

	def RedOrGreen(self, N, S):
		ans = 0
		if S.count('R') > S.count('G'):
			return S.count('G')
		else:
			return S.count('R')


from collections import Counter

class Solution:

	def RedOrGreen(self, N, S):
		return min(S.count('R'), S.count('G'))


from collections import Counter

class Solution:

	def RedOrGreen(self, N, S):
		x = Counter(S)
		if len(x) == 1:
			return 0
		else:
			return min(x.values())


class Solution:

	def RedOrGreen(self, N, S):
		count = 0
		abc = 0
		for i in S:
			if i == 'R':
				count += 1
			else:
				abc += 1
		if count > abc:
			return abc
		return count


class Solution:

	def RedOrGreen(self, N, S):
		count1 = 0
		count2 = 0
		for i in S:
			if i == 'R':
				count1 += 1
			if i == 'G':
				count2 += 1
		return min(count1, count2)


class Solution:

	def RedOrGreen(self, N, S):
		l1 = []
		l2 = []
		for i in range(len(S)):
			if S[i] == 'R':
				l1.append(i)
			else:
				l2.append(i)
		if len(l1) > len(l2):
			return len(l2)
		else:
			return len(l1)


class Solution:

	def RedOrGreen(self, N, S):
		c = S.count('R')
		return min(c, N - c)


class Solution:

	def RedOrGreen(self, N, S):
		countr = 0
		countg = 0
		for i in S:
			if i is 'R':
				countr = countr + 1
			if i is 'G':
				countg = countg + 1
		return min(countr, countg)


class Solution:

	def RedOrGreen(self, N, S):
		d = {'R': 0, 'G': 0}
		for x in S:
			d[x] += 1
		return min(d.values())


class Solution:

	def RedOrGreen(self, N, S):
		greencount = 0
		redcount = 0
		for i in S:
			if i == 'G':
				greencount += 1
			else:
				redcount += 1
		if greencount > redcount:
			return redcount
		else:
			return greencount


class Solution:

	def RedOrGreen(self, N, S):
		l = []
		a = set(S)
		for i in a:
			l.append(S.count(i))
		if len(l) == 1:
			return 0
		else:
			return min(l)


class Solution:

	def RedOrGreen(self, N, S):
		x = S.count('R')
		y = S.count('G')
		z = max(x, y)
		return N - z