Given the maximum occurrences of a, b, and c in a string. Your task is to make the string containing only a, b, and c such that no three consecutive characters are the same. If the resultant string equals to a+b+c, return the length (a+b+c) otherwise -1.
Example 1:
Input:
a = 3, b = 3, c = 3
Output: 
9
Explanation: 
No three consecutive character of
the string "abcabcabc" is same.
Example 2:
Input:
a = 11, b = 2, c = 2
Output: 
-1
Explanation: 
If we build a string of these character's,
the string will be"aabaacaabaacaaa", here
we can see that there will be three consecutive a. So
there is no solution exist.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function solve( ) which takes integers a, b, and c as input parameters and returns the string length. If there is no possible answer return -1.
Expected Time Complexity: O(a+b+c)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ a, b, c ≤ 10^{5}
0 < (a + b + c)

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution():

    def solve(self, a, b, c):

        #your code goes here

Solutions:
class Solution:

	def solve(self, a, b, c):
		if a > (b + c + 1) * 2 or b > (c + a + 1) * 2 or c > (a + b + 1) * 2:
			return -1
		return a + b + c


class Solution:

	def solve(self, a, b, c):
		consecutiveA = consecutiveB = consecutiveC = 0
		ans = ''
		total = a + b + c
		length = total
		while total > 0:
			if a >= b and a >= c and (consecutiveA != 2) or (a > 0 and (consecutiveB == 2 or consecutiveC == 2)):
				a -= 1
				consecutiveB = 0
				consecutiveC = 0
				consecutiveA += 1
				ans += 'a'
			elif b >= a and b >= c and (consecutiveB != 2) or (b > 0 and (consecutiveA == 2 or consecutiveC == 2)):
				b -= 1
				consecutiveA = 0
				consecutiveC = 0
				consecutiveB += 1
				ans += 'b'
			elif c >= a and c >= b and (consecutiveC != 2) or (c > 0 and (consecutiveA == 2 or consecutiveB == 2)):
				ans += 'c'
				c -= 1
				consecutiveB = 0
				consecutiveA = 0
				consecutiveC += 1
			total -= 1
		return length if a == 0 and c == 0 and (b == 0) else -1


class Solution:

	def solve(self, a, b, c):
		l = [a, b, c]
		l.sort()
		if l[-1] >= 3 + 2 * (l[0] + l[1]):
			return -1
		return a + b + c


class Solution:

	def solve(self, a, b, c):
		maxNum = max(a, b, c)
		if maxNum <= 2 * (a + b + c - maxNum + 1):
			return a + b + c
		else:
			return -1


class Solution:

	def solve(self, a, b, c):
		maximum = max(a, b, c)
		if maximum == a:
			y = b + c
		elif maximum == b:
			y = a + c
		else:
			y = b + a
		if 2 * (y + 1) < maximum:
			return -1
		return a + b + c


class Solution:

	def solve(self, a, b, c):
		arr = [a, b, c]
		arr.sort(reverse=True)
		if arr[0] / 2 > arr[1] + arr[2] + 1:
			return -1
		else:
			return sum(arr)


class Solution:

	def solve(self, a, b, c):
		if a == b == c:
			return a + b + c
		else:
			s = sorted([a, b, c])
			k = s[0] + s[1]
			if s[2] - k * 2 >= 3:
				return -1
			else:
				return a + b + c


class Solution:

	def solve(self, a, b, c):
		nums = [a, b, c]
		nums.sort()
		low = min(a, b, c)
		high = max(a, b, c)
		if high > (low + nums[1] + 1) * 2:
			return -1
		return a + b + c


class Solution:

	def solve(self, a, b, c):
		arr = [a, b, c]
		arr.sort()
		if (arr[0] + arr[1] + 1) * 2 >= arr[2]:
			return a + b + c
		else:
			return -1


class Solution:

	def solve(self, a, b, c):
		v = [['a', a], ['b', b], ['c', c]]
		ans = ''
		prev = -1
		while 1:
			m = 0
			for i in range(3):
				if v[prev][0] != v[i][0] and v[i][1] > m:
					cur = i
					m = v[i][1]
			if m == 0:
				break
			ans += v[cur][0]
			v[cur][1] -= 1
			if m >= 2 and (v[prev][0] == -1 or m > v[prev][1]):
				ans += v[cur][0]
				v[cur][1] -= 1
			prev = cur
		if len(ans) == a + b + c:
			return len(ans)
		else:
			return -1


class Solution:

	def solve(self, a, b, c):
		mx = max(a, max(b, c))
		mn = min(a, min(b, c))
		md = a + b + c - mx - mn
		if mx > (md + mn + 1) * 2:
			return -1
		return a + b + c


class Solution:

	def solve(self, a, b, c):
		s = a + b + c
		rc = s - max(a, b, c)
		if max(a, b, c) > 2 * (rc + 1):
			return -1
		else:
			return s


class Solution:

	def solve(self, a, b, c):
		import math
		d = max(a, b, c)
		e = min(a, b, c)
		f = a + b + c - d - e
		if d % 2 == 0:
			if e + f < math.floor(d // 2) - 1:
				return -1
			else:
				return a + b + c
		elif e + f < math.floor(d / 2):
			return -1
		else:
			return a + b + c


class Solution:

	def solve(self, a, b, c):
		A = max(a, b, c)
		if A == b:
			b = a
		if A == c:
			c = a
		if A <= 2 * (b + c) + 2:
			return A + b + c
		return -1


class Solution:

	def solve(self, a, b, c):
		import math
		if b + c < math.ceil(a / 2) - 1 or a + c < math.ceil(b / 2) - 1 or a + b < math.ceil(c / 2) - 1:
			return -1
		else:
			return a + b + c


import math

class Solution:

	def solve(self, a, b, c):
		if a == b and b == c:
			return a * 3
		else:
			x = 0
			if a > b and a > c:
				if math.ceil(a // 2) >= a - (b + c + 1):
					return a + b + c
			elif b > c:
				if math.ceil(b // 2) >= b - (a + c + 1):
					return a + b + c
			elif math.ceil(c // 2) >= c - (b + a + 1):
				return a + b + c
		return -1


class Solution:

	def solve(self, a, b, c):
		tab = [a, b, c]
		tab.sort()
		if tab[2] > 2 * (tab[0] + tab[1]) + 2:
			return -1
		return sum(tab)


class Solution:

	def solve(self, a, b, c):
		l = [a, b, c]
		l.sort(reverse=True)
		m = l[0] // 2 + 1 if l[0] % 2 != 0 else l[0] // 2
		if m - 1 > l[1] + l[2]:
			return -1
		return a + b + c


class Solution:

	def solve(self, a, b, c):
		d = [a, b, c]
		x = max(d)
		d.remove(x)
		sum = d[0] + d[1]
		if x > 2 * sum + 2:
			return -1
		else:
			return sum + x


class Solution:

	def solve(self, a, b, c):
		le = [a, b, c]
		le.sort(reverse=True)
		maxi = le[0]
		if maxi <= (le[1] + le[2]) * 2 + 2:
			return a + b + c
		else:
			return -1


class Solution:

	def solve(self, a, b, c):
		if a >= b and a >= c:
			maxi = a
			other = b + c
		elif b >= c and b >= a:
			maxi = b
			other = a + c
		elif c >= a and c >= b:
			maxi = c
			other = a + b
		if maxi > 2 * (other + 1):
			return -1
		else:
			return sum([a, b, c])


class Solution:

	def solve(self, a, b, c):
		if a <= 2 * (1 + b + c) and c <= 2 * (1 + a + b) and (b <= 2 * (1 + a + c)):
			return a + b + c
		else:
			return -1


class Solution:

	def solve(self, a, b, c):
		prev = -1
		v = [a, b, c]
		ans = ''
		while 1:
			ma = 0
			cur = 0
			for i in range(3):
				if prev != i + 97 and ma < v[i]:
					ma = v[i]
					cur = i + 97
			if ma == 0:
				break
			ans += chr(cur)
			v[cur - 97] -= 1
			if ma >= 2 and (prev == -1 or ma > v[prev - 97]):
				ans += chr(cur)
				v[cur - 97] -= 1
			prev = cur
			n = len(ans)
		if n != a + b + c:
			return -1
		return n


class Solution:

	def solve(self, a, b, c):
		ans = a + b + c
		string = 'abc'
		lowest = min(min(a, b), c)
		string *= lowest
		a -= lowest
		b -= lowest
		c -= lowest
		next = 0
		if a == 0:
			next = min(b, c)
			string += 'bc' * next
			b -= next
			c -= next
		elif b == 0:
			next = min(a, c)
			string += 'ac' * next
			a -= next
			c -= next
		elif c == 0:
			next = min(a, b)
			string += 'ab' * next
			a -= next
			b -= next
		if a != 0:
			i = -1
			while i < len(string) - 1:
				if string[i + 1] == 'a':
					a -= 1
					i += 2
				else:
					a -= 2
					i += 1
			if len(string) > 0 and string[-1] != 'a':
				a -= 2
		elif b != 0:
			i = -1
			while i < len(string) - 1:
				if string[i + 1] == 'b':
					b -= 1
					i += 2
				else:
					b -= 2
					i += 1
			if len(string) > 0 and string[-1] != 'b':
				b -= 2
		elif c != 0:
			i = -1
			while i < len(string) - 1:
				if string[i + 1] == 'c':
					c -= 1
					i += 2
				else:
					c -= 2
					i += 1
			if len(string) > 0 and string[-1] != 'c':
				c -= 2
		if a > 0 or b > 0 or c > 0:
			return -1
		return ans


class Solution:

	def solve(self, a, b, c):
		li = [a, b, c]
		li.sort(reverse=True)
		if li[0] <= 2 * (li[1] + li[-1]):
			return sum(li)
		if li[0] - 2 * (li[1] + li[-1]) > 2:
			return -1
		return 3 * (li[1] + li[-1]) + min(2, li[0] - 2 * (li[1] + li[-1]))


class Solution:

	def solve(self, a, b, c):
		p = max(a, b, c)
		l = a + b + c - p
		if a == b and b == c:
			return a + b + c
		elif a < 3 and b < 3 and (c < 3):
			return a + b + c
		elif (l + 1) * 2 >= p:
			return a + b + c
		else:
			return -1


class Solution:

	def solve(self, a, b, c):
		maxi = max([a, b, c])
		if maxi == a:
			temp = (b + c) * 2
			if temp + 2 < a:
				return -1
			else:
				return a + b + c
		elif maxi == b:
			temp = (a + c) * 2
			if temp + 2 < b:
				return -1
			else:
				return a + b + c
		else:
			temp = (a + b) * 2
			if temp + 2 < c:
				return -1
			else:
				return a + b + c


class Solution:

	def solve(self, a, b, c):
		s = a + b + c
		if a >= 3 + 2 * (b + c) or b >= 3 + 2 * (c + a) or c >= 3 + 2 * (a + b):
			return -1
		return s

class Solution1:

	def solve(self, a, b, c):
		lst = [a, b, c]
		lst.sort()
		if lst[-1] >= 3 + 2 * (lst[0] + lst[1]):
			return -1
		return sum(lst)


class Solution:

	def solve(self, a, b, c):
		l = a
		m = b
		s = c
		if a > b and a > c:
			if b < c:
				m = c
				s = b
			else:
				m = b
				s = c
		elif b > a and b > c:
			l = b
			if a < c:
				m = c
				s = a
			else:
				m = a
				s = c
		elif c > a and c > b:
			l = c
			if a < b:
				m = b
				s = a
			else:
				m = a
				s = b
		x = m + s
		while l >= 3:
			l = l - 2
			x = x - 1
		if x < 0:
			return -1
		else:
			return a + b + c


class Solution:

	def solve(self, a, b, c):
		arr = [a, b, c]
		arr.sort()
		(mini, mid, maxi) = tuple(arr)
		if maxi > (mini + mid + 1) * 2:
			return -1
		return sum(arr)


class Solution:

	def solve(self, a, b, c):
		l = 0
		sum = 0
		res = 0
		s = []
		if a > b and a > c:
			l = a
			s.extend([b, c])
		elif b > a and b > c:
			l = b
			s.extend([a, c])
		elif c > a and c > b:
			l = c
			s.extend([a, b])
		else:
			s.extend([a, b, c])
		for i in range(len(s)):
			sum = sum + s[i]
		if 2 * sum + 2 >= l:
			res = sum + l
			return int(res)
		else:
			return -1


class Solution:

	def solve(self, a, b, c):
		highest = max(a, b, c)
		total = a + b + c
		x = highest // 2 - 1
		if highest % 2 != 0:
			x += 1
		if x <= total - highest:
			return total
		else:
			return -1


class Solution:

	def solve(self, a, b, c):
		m = max(a, b, c)
		if m > 2 * (a + b) + 2 or m > 2 * (b + c) + 2 or m > 2 * (a + c) + 2:
			return -1
		else:
			return a + b + c