There is a table on which N balls are kept starting from index 1 to N in horizontal direction. Each ball is either of  red (denoted by 'R') or of blue (denoted by 'B') color. Any red ball which is placed on even indices and blue balls placed on odd indices is considered as wrongly placed. You need return the number of balls placed wrong on the table.
 
Example 1:
Input:
S = "RRBB"
Output: 2
Explanation: second R and first B
is wrongly placed.
Ã¢â¬â¹
Example 2:
Input: 
S = "RBRB"
Output: 0
Explanation: No ball is wrongly placed.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countWrongPlacedBalls() which accpets a string as input parameter and returns the number of wrongly placed balls.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1<= N <=10^{5}
S[i] = 'R' or S[i] = 'B'

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def countWrongPlacedBalls(self, s):
        # code here

Solutions:
class Solution:

	def countWrongPlacedBalls(self, s):
		count = 0
		for i in range(len(s)):
			if (i + 1) % 2 == 0 and s[i] == 'R':
				count += 1
			if (i + 1) % 2 != 0 and s[i] == 'B':
				count += 1
		return count


class Solution:

	def countWrongPlacedBalls(self, s):
		(r, b) = (0, 0)
		for i in range(len(s)):
			if i % 2 == 0:
				if s[i] != 'R':
					r += 1
			elif i % 2 != 0:
				if s[i] != 'B':
					b += 1
		return r + b


class Solution:

	def countWrongPlacedBalls(self, s):
		n = len(s)
		wrong = 0
		for i in range(n):
			if i % 2 == 0 and s[i] == 'R':
				continue
			elif i % 2 != 0 and s[i] == 'B':
				continue
			else:
				wrong += 1
		return wrong


class Solution:

	def countWrongPlacedBalls(self, s):
		count = 0
		for i in range(len(s)):
			if s[i] == 'R':
				if (i + 1) % 2 == 0:
					count += 1
				else:
					continue
			elif s[i] == 'B':
				if (i + 1) % 2 != 0:
					count += 1
				else:
					continue
		return count


class Solution:

	def countWrongPlacedBalls(self, s):
		k = 0
		for i in range(len(s)):
			if i % 2 == 1 and s[i] == 'R':
				k += 1
			elif i % 2 == 0 and s[i] == 'B':
				k += 1
			else:
				pass
		return k


class Solution:

	def countWrongPlacedBalls(self, s):
		n = len(s)
		wrong = 0
		for i in range(n):
			if i % 2 == 0 and s[i] == 'R':
				continue
			elif i % 2 != 0 and s[i] == 'B':
				continue
			else:
				wrong += 1
		return wrong
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ob = Solution()
		print(ob.countWrongPlacedBalls(s))


class Solution:

	def countWrongPlacedBalls(self, s):
		count = 0
		for i in range(len(s)):
			if s[i] == 'R':
				if i % 2 == 1:
					count += 1
			elif i % 2 == 0:
				count += 1
		return count
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ob = Solution()
		print(ob.countWrongPlacedBalls(s))


class Solution:

	def countWrongPlacedBalls(self, s):
		n = len(s)
		wc = 0
		for i in range(n):
			if s[i] == 'R' and i % 2 != 0:
				wc += 1
			elif s[i] == 'B' and i % 2 == 0:
				wc += 1
		return wc


class Solution:

	def countWrongPlacedBalls(self, s):
		c = 0
		for i in range(len(s)):
			if s[i] == 'R':
				if i % 2 == 0:
					pass
				else:
					c = c + 1
			elif i % 2 == 0:
				c = c + 1
			else:
				pass
		return c


class Solution:

	def countWrongPlacedBalls(self, s):
		x = s
		strl = ['k']
		count = 0
		for i in range(len(x)):
			strl.append(x[i])
		for i in range(1, len(strl)):
			if i % 2 == 1:
				if strl[i] == 'B':
					count += 1
			elif strl[i] == 'R':
				count += 1
		return count


class Solution:

	def countWrongPlacedBalls(self, s):
		num = 0
		for (i, color) in enumerate(s, start=1):
			if color == 'R' and i % 2 == 0:
				num += 1
			if color == 'B' and i % 2 == 1:
				num += 1
		return num


class Solution:

	def countWrongPlacedBalls(self, s):
		i = 1
		count = 0
		for x in range(len(s)):
			if i:
				if s[x] == 'B':
					count += 1
				i = 0
			else:
				if s[x] == 'R':
					count += 1
				i = 1
		return count