Given an array s containing n strings, find the length of the string obtained by combining the strings. Two strings can be only combined if the last character of the first string and the first character of the second string are the same.
Example 1:
Input:
n = 3
s[] = {"RBR", "BBR", "RRR"}
Output: 9
Explanation: Combined string can 
             be: BRR + RBR + RRR
Example 2:
Input:
n = 2
s[] = {"RRR", "BBB"}
Output: 0
Explanation: Since combining condition
             isn't fulfilled answer is 0.
Your Task:
Your task is to complete the function combine() which takes 2 arguments(integer n and array of n strings) and returns the maximum length of combined strings possible. If no 2 strings can be combined return 0. 
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
2<=n<=1000
1<=|s_{i}|<=1000, where 0<=i<n
s will only contain {R,B}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:



    def combine(self, n,s):

        # code here





Solutions:
class Solution:

	def combine(self, n, s):
		BB = RR = BR = RB = 0
		for i in s:
			if i[0] + i[-1] == 'BB':
				BB = BB + 1
			elif i[0] + i[-1] == 'RR':
				RR = RR + 1
			elif i[0] + i[-1] == 'RB':
				RB = RB + 1
			elif i[0] + i[-1] == 'BR':
				BR = BR + 1
		mn = min(BR, RB)
		ans = 0
		if not BR and (not RB):
			ans = max(BB, RR)
		elif RB == BR:
			ans = BB + RR + 2 * mn
		else:
			ans = BB + RR + 2 * mn + 1
		ans *= len(s[0])
		return 0 if ans == len(s[0]) else ans


class Solution:

	def combine(self, n, s):
		rr = 0
		bb = 0
		rb = 0
		br = 0
		l = len(s[0])
		ans = 0
		for i in s:
			if i[0] == 'R':
				if i[-1] == 'R':
					rr += 1
				else:
					rb += 1
			elif i[-1] == 'R':
				br += 1
			else:
				bb += 1
		if rb == 0 and br == 0:
			ans = max(rr, bb)
		elif rb == br:
			ans = rr + bb + 2 * rb
		else:
			ans = rr + bb + 2 * min(rb, br) + 1
		if ans == 1:
			return 0
		return ans * l


class Solution:

	def combine(self, n, s):
		lenRB = 0
		lenBR = 0
		lenRR = 0
		lenBB = 0
		countRB = 0
		countBR = 0
		countRR = 0
		countBB = 0
		perLen = len(s[0])
		for si in s:
			if si[0] != si[-1]:
				if si[0] == 'B':
					lenBR += len(si)
					countBR += 1
				else:
					lenRB += len(si)
					countRB += 1
			elif si[0] == 'B':
				lenBB += len(si)
				countBB += 1
			else:
				lenRR += len(si)
				countRR += 1
		if lenRB != 0 or lenBR != 0:
			count = min(countRB, countBR) * 2
			if lenRB != lenBR:
				count += 1
			totalLen = perLen * count
			if count + countRR + countBB > 1:
				return totalLen + lenRR + lenBB
		elif lenRR > lenBB and countRR > 1:
			return lenRR
		elif countBB > 1:
			return lenBB
		return 0


class Solution:

	def combine(self, n, s):
		count = len(s[0])
		(rr, rb, br, bb) = (0, 0, 0, 0)
		for i in range(len(s)):
			if s[i][0] == 'R' and s[i][-1] == 'R':
				rr += 1
			elif s[i][0] == 'R' and s[i][-1] == 'B':
				rb += 1
			elif s[i][0] == 'B' and s[i][-1] == 'R':
				br += 1
			elif s[i][0] == 'B' and s[i][-1] == 'B':
				bb += 1
		m = min(rb, br)
		if rb == 0 and br == 0:
			count = max(rr, bb)
		elif rb == br:
			count = rr + bb + 2 * m
		else:
			count = rr + bb + 2 * m + 1
		count *= len(s[0])
		if count == len(s[0]):
			return 0
		else:
			return count