Given binary string str consisting of only 0's and 1's, Check if all the 0's are together or not.
Example 1:
Input:
str = "000"
Output:
YES
Explanation:
All the 0's are together.
Example 2:
Input:
str = "111"
Output:
NO
Explanation:
All the 0's are not together.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function checkTogether() which takes the binary string str as an input parameter and returns 1 if all the 0's are together else returns 0.
Expected Time Complexity: O(N), Where N is the size of the string.
Expected Auxiliary Space: O(1)
Constraints:
1 <= |str| <= 10000

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def checkTogether(self, str):
        # code here

Solutions:
class Solution:

	def checkTogether(self, str):
		temp = False
		for i in range(len(str)):
			if str[i] == '0' and (not temp):
				temp = True
			elif str[i] == '1' and temp == True:
				z = str[i:len(str)]
				if '0' in z:
					temp = False
					break
		if temp:
			return True
		else:
			return False


class Solution:

	def checkTogether(self, str):
		if '0' in str:
			i = str.index('0')
			j = str.rindex('0') + 1
			return not '1' in str[i:j]


class Solution:

	def checkTogether(self, str):
		if '0' in str:
			first_ind = str.index('0')
			last_ind = str.rindex('0')
			return '1' not in str[first_ind:last_ind]


class Solution:

	def checkTogether(self, str):
		if '0' not in str:
			return False
		first = str.index('0')
		s = str
		s = s[::-1]
		index = s.index('0')
		last = len(s) - index - 1
		if first == -1:
			return False
		for i in range(first, last):
			if str[i] == '1':
				return False
		return True


class Solution:

	def checkTogether(self, str):
		if '0' not in str:
			return False
		else:
			str1 = str[::-1]
			first0 = str.index('0')
			second0 = len(str1) - str1.index('0') - 1
			if str.count('0') == second0 - first0 + 1:
				return True
			return False


class Solution:

	def checkTogether(self, str):
		if '0' not in str:
			return 0
		elif '0' in str:
			count = str.count('0')
			value = '0' * count
			if value in str:
				return 1


class Solution:

	def checkTogether(self, str):
		stack = [*str]
		while stack and stack[-1] == '1':
			stack.pop()
		if not stack:
			return 0
		while stack and stack[-1] == '0':
			stack.pop()
		while stack and stack[-1] == '1':
			stack.pop()
		return not stack


class Solution:

	def checkTogether(self, str):
		zero_flag = False
		for i in range(len(str)):
			if str[i] == '0':
				zero_flag = True
			elif str[i] == '1' and zero_flag:
				if '0' in str[i:len(str)]:
					zero_flag = False
					break
		return 1 if zero_flag else 0


class Solution:

	def checkTogether(self, str):
		k = 0
		if '0' not in s:
			return 0
		for i in range(len(str) - 1):
			if str[i] == '0' and str[i + 1] == '0':
				continue
			elif str[i] == '0' and str[i + 1] == '1' and (i + 1 == len(s) - 1):
				k = 0
				break
			elif str[i] == '0' and str[i + 1] == '1' and ('0' in str[i + 1:]):
				k = 1
				break
		if k == 0:
			return 1
		else:
			return 0


class Solution:

	def checkTogether(self, str):
		a = str.count('1')
		if a == len(str):
			return False
		one = 0
		zero = 0
		for i in range(0, len(str)):
			if str[i] == '0':
				zero += 1
				if zero == 2:
					zero = 0
			if str[i] == '1' and zero == 0:
				pass
			if str[i] == '1' and zero == 1:
				one += 1
			if zero > 0 and one > 1:
				return False
		return True


class Solution:

	def checkTogether(self, str):
		one = False
		zero = False
		for i in str:
			if i == '0':
				zero = True
			elif zero and i == '1':
				one = True
			if one and i == '0':
				zero = False
				break
		return zero


class Solution:

	def checkTogether(self, str):
		if '0' not in str:
			return False
		f = str.index('0')
		s = str
		s = s[::-1]
		l = s.index('0')
		h = len(s) - l - 1
		if f == -1:
			return False
		for i in range(f, h):
			if str[i] == '1':
				return False
		return True


class Solution:

	def checkTogether(self, str):
		try:
			zeros = str.count('0')
			z1 = str.index('0')
		except:
			return False
		c = 0
		while z1 < len(str) and str[z1] == '0':
			c += 1
			z1 += 1
		if c == zeros:
			return True
		return False
		print(c, zeros)


class Solution:

	def checkTogether(self, str):
		flag = False
		count = 0
		zero = 0
		if len(str) == 1:
			if str[0] == '0':
				return True
			else:
				return False
		for i in range(len(str) - 1):
			if s[i] == '0':
				zero += 1
			if str[i] == '1' and str[i + 1] == '0':
				flag = True
				count += 1
				if flag == True and count > 1:
					return False
		if zero == 0:
			return False
		return True
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ob = Solution()
		if ob.checkTogether(s):
			print('YES')
		else:
			print('NO')


class Solution:

	def checkTogether(self, str):
		c = 0
		for i in range(len(str)):
			if str[i] == '0':
				c += 1
			if c > 1 and str[i] == '1':
				break
		if c == str.count('0'):
			return c
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ob = Solution()
		if ob.checkTogether(s):
			print('YES')
		else:
			print('NO')


class Solution:

	def checkTogether(self, str):
		c = 0
		d = 0
		e = 0
		for i in str:
			if i == '0':
				c += 1
				e += 1
			else:
				if c > d:
					d = c
				c = 0
		if c > d:
			d = c
		if d == 0 or e != d:
			return False
		return True
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ob = Solution()
		if ob.checkTogether(s):
			print('YES')
		else:
			print('NO')


class Solution:

	def checkTogether(self, str):
		if '0' in str:
			ct = str.count('0')
			si = str.index('0')
			ss = str[::-1]
			ei = len(ss) - 1 - ss.index('0')
			if si + ct - 1 == ei:
				return True
			else:
				return False
		else:
			return False
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ob = Solution()
		if ob.checkTogether(s):
			print('YES')
		else:
			print('NO')


class Solution:

	def checkTogether(self, s):
		binary = list(s)
		if '0' not in binary:
			return False
		indx1 = binary.index('0')
		binary.reverse()
		indx2 = len(binary) - 1 - binary.index('0')
		binary.reverse()
		if '1' in binary[indx1:indx2 + 1]:
			return False
		else:
			return True
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ob = Solution()
		if ob.checkTogether(s):
			print('YES')
		else:
			print('NO')


class Solution:

	def checkTogether(self, str):
		if '0' not in str:
			return False
		first = str.index('0')
		s = str
		s = s[::-1]
		index = s.index('0')
		last = len(s) - index - 1
		if first == -1:
			return False
		for i in range(first, last):
			if str[i] == '1':
				return False
		return True
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ob = Solution()
		if ob.checkTogether(s):
			print('YES')
		else:
			print('NO')


class Solution:

	def checkTogether(self, str):
		for i in range(len(str) - 2):
			if '0' not in str:
				return False
			elif '1' not in str:
				return True
			elif str[i] == '0' and str[i + 1] == '1' and (str[i + 2] == '0'):
				return False
		return True


class Solution:

	def checkTogether(self, str):
		flag = 0
		for i in range(1, len(str)):
			if str[i] == '1' and str[i - 1] == '0':
				flag = 1
			if str[i] == '0' and str[i - 1] == '1' and (flag == 1):
				return False
		if str.count('0') == 0:
			return False
		return True


class Solution:

	def checkTogether(self, s):
		n = len(s)
		c0 = s.count('0')
		if c0 == 0:
			return False
		sub = ''
		for i in range(n):
			if s[i] == '0':
				sub = s[i:i + c0]
				break
		if sub == '':
			return False
		for ch in sub:
			if ch != '0':
				return False
		return True


class Solution:

	def checkTogether(self, str):
		flage = False
		index = 0
		n = len(str)
		if '0' not in str:
			return False
		while index < n:
			if str[index] == '0':
				if flage == True:
					return False
				while index < n and str[index] == '0':
					index += 1
				flage = True
			else:
				index += 1
		return True


class Solution:

	def checkTogether(self, str):
		si = -1
		ei = -1
		for i in range(len(str)):
			if str[i] == '0':
				si = i
				break
		for i in range(len(str) - 1, -1, -1):
			if str[i] == '0':
				ei = i
				break
		if si == -1 or ei == -1:
			return 0
		i = si
		for i in range(si, ei):
			if str[i] == '1':
				return 0
		return 1


class Solution:

	def checkTogether(self, str):
		i = 0
		j = 1
		k = 2
		if len(str) == 1 and str[i] == '0':
			return True
		elif len(str) == 2 and (str[i] == '0' or str[j] == '0'):
			return True
		elif len(str) == str.count('1'):
			return False
		while k < len(str):
			if str[i] == '0' and str[j] == '1' and (str[k] == '0'):
				return False
			i += 1
			j += 1
			k += 1
		return True


class Solution:

	def checkTogether(self, str):
		l = list(str)
		d = {}
		k = []
		for i in range(0, len(str)):
			if str[i] == '0':
				k.append(i)
			else:
				pass
		j = 0
		c = 0
		if len(k) == 0:
			return 0
		else:
			for i in k:
				try:
					if i + 1 == k[j + 1]:
						pass
					else:
						return 0
					j += 1
				except:
					pass
			if c == 0:
				return 1


class Solution:

	def checkTogether(self, str):
		start_1_idx = -1
		end_1_idx = -1
		for i in range(len(s)):
			if s[i] == '0':
				start_1_idx = i
				break
		for i in range(len(s) - 1, -1, -1):
			if s[i] == '0':
				end_1_idx = i
				break
		i = start_1_idx
		for i in range(start_1_idx, end_1_idx):
			if s[i] == '1':
				return False
		if start_1_idx == -1 and end_1_idx == -1:
			return False
		return True