Given a binary string s and two integers x and y. Arrange the given string in such a way so that '0' comes X-times then '1' comes Y-times and so on until one of the '0' or '1' is finished. Then concatenate the rest of the string and find the final string.
Example 1:
Input:
X = 1, Y = 1, S = "0011"
Output: "0101"
Explanation: we put 1 '0' and 1 '1'
alternatively.
Example 2:
Input:
X = 1, Y = 1, S = "1011011"
Output: "0101111"
Explanation: We put '0' and '1' 
alternatively and in last we have to
put all '1' as there is no '0' left.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function arrangeString() which takes the string S and X, Y as inputs, and returns modified string.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ X, Y ≤ 10^{3}
1 ≤ |S| ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def arrangeString(self, s, x, y):
        # code here

Solutions:
class Solution:

	def arrangeString(self, s, x, y):
		zeros = 0
		ones = 0
		for item in s:
			zeros += item == '0'
			ones += item == '1'
		output = ''
		while ones and zeros:
			i = 0
			while i < x and zeros:
				output += '0'
				i += 1
				zeros -= 1
			i = 0
			while i < y and ones:
				output += '1'
				i += 1
				ones -= 1
		return output + zeros * '0' + ones * '1'


class Solution:

	def arrangeString(self, s, x, y):
		zero = 0
		one = 0
		n = len(s)
		for i in range(n):
			if s[i] == '0':
				zero += 1
			else:
				one += 1
		new_str = ''
		while zero > 0 or one > 0:
			if zero >= x:
				new_str += '0' * x
				zero -= x
			elif zero > 0:
				new_str += '0' * zero
				zero = 0
			if one >= y:
				new_str += '1' * y
				one -= y
			elif one > 0:
				new_str += '1' * one
				one = 0
		return new_str


class Solution:

	def arrangeString(self, s, x, y):
		arr = list(s)
		zero_cnt = 0
		one_cnt = 0
		for elem in arr:
			if elem == '0':
				zero_cnt += 1
			else:
				one_cnt += 1
		index = 0
		while zero_cnt > 0 and one_cnt > 0:
			for i in range(0, min(x, zero_cnt)):
				arr[index] = '0'
				index += 1
				zero_cnt -= 1
			for i in range(0, min(y, one_cnt)):
				arr[index] = '1'
				index += 1
				one_cnt -= 1
		if zero_cnt > 0:
			for i in range(0, zero_cnt):
				arr[index] = '0'
				index += 1
		if one_cnt > 0:
			for i in range(0, one_cnt):
				arr[index] = '1'
				index += 1
		return ''.join(arr)


class Solution:

	def arrangeString(self, s, x, y):
		(cz, co) = (0, 0)
		k1 = 0
		k2 = 0
		res = ''
		for i in s:
			if i == '0':
				cz += 1
			if i == '1':
				co += 1
		if cz < x:
			res += '0' * cz
			cz = 0
		if co < y:
			if cz != 0 and cz >= x:
				res += '0' * x
				cz -= x
				res += '1' * co
				co = 0
		while cz != 0 or co != 0:
			if cz >= x:
				res += '0' * x
				cz -= x
			if co >= y:
				if len(res) > 0:
					res += '1' * y
					co -= y
			if cz < x:
				res += '0' * cz
				cz = 0
			if co < y:
				if cz >= x:
					res += '0' * x
					cz -= x
				res += '1' * co
				co = 0
			if cz != 0 and co == 0:
				res += '0' * cz
				cz = 0
			if co != 0 and cz == 0:
				res += '1' * co
				co = 0
		return res


class Solution:

	def arrangeString(self, s, x, y):
		(count_zero, count_one) = (0, 0)
		for i in s:
			if i == '0':
				count_zero += 1
			else:
				count_one += 1
		ans = ''
		n = len(s)
		i = 0
		while count_zero > 0 and count_one > 0:
			for i in range(x):
				if count_zero > 0:
					ans += '0'
					count_zero -= 1
			for i in range(y):
				if count_one > 0:
					ans += '1'
					count_one -= 1
		if count_zero > 0:
			ans += count_zero * '0'
		if count_one > 0:
			ans += count_one * '1'
		return ans


class Solution:

	def arrangeString(self, s, x, y):
		m = ''
		c1 = s.count('0')
		c2 = s.count('1')
		while c1 > 0 or c2 > 0:
			for i in range(0, x):
				if c1 > 0:
					m += '0'
					c1 -= 1
			for j in range(0, y):
				if c2 > 0:
					m += '1'
					c2 -= 1
		return m


class Solution:

	def arrangeString(self, s, x, y):
		count1 = 0
		count2 = 0
		for i in range(len(s)):
			if s[i] == '0':
				count1 += 1
			elif s[i] == '1':
				count2 += 1
		st = ''
		temp1 = x
		temp2 = y
		i = 0
		while len(st) < len(s) and count1 > 0 and (count2 > 0):
			while x and count1 > 0:
				st += '0'
				count1 -= 1
				x -= 1
			while y and count2 > 0:
				st += '1'
				count2 -= 1
				y -= 1
			x = temp1
			y = temp2
		if count1 != 0:
			for i in range(count1):
				st += '0'
		elif count2 != 0:
			for i in range(count2):
				st += '1'
		return st


from collections import Counter

class Solution:

	def arrangeString(self, s, x, y):
		ans = ''
		gg = Counter(s)
		c = gg['0']
		d = gg['1']
		a = ''
		b = ''
		while c > 0 or d > 0:
			if c >= x:
				for i in range(x):
					ans += str(0)
				c -= x
			else:
				for i in range(c):
					ans += str(0)
				c = 0
			if d >= y:
				for i in range(y):
					ans += str(1)
				d -= y
			else:
				for i in range(d):
					ans += str(1)
				d = 0
		return ans


class Solution:

	def arrangeString(self, s, x, y):
		os = s.count('1')
		zs = s.count('0')
		output = ''
		while zs > 0 or os > 0:
			for j in range(x):
				if zs > 0:
					output += '0'
					zs -= 1
			for k in range(y):
				if os > 0:
					output += '1'
					os -= 1
		return output


class Solution:

	def arrangeString(self, s, x, y):
		z = 0
		o = 0
		for i in range(len(s)):
			if s[i] == '1':
				o += 1
			elif s[i] == '0':
				z += 1
		nS = ''
		while o > 0 or z > 0:
			for i in range(x):
				if z > 0:
					nS += '0'
					z -= 1
			for j in range(y):
				if o > 0:
					nS += '1'
					o -= 1
		return nS