Given a non-empty sequence of characters str, return true if sequence is Binary, else return false
Example 1:
Input:
str = 101
Output:
1
Explanation:
Since string contains only 0 and 1, output is 1.
Example 2:
Input:
str = 75
Output:
0
Explanation:
Since string contains digits other than 0 and 1, output is 0.
 
Your Task:
Complete the function isBinary() which takes an string str as input parameter and returns 1 if str is binary and returns 0 otherwise.
 
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <=T<= 50
1 <=Length of str<= 10000

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: # Return true if str is binary, else false
def isBinary(str):
    #code here

Solutions:
def isBinary(str):
	c = 0
	for i in str:
		if i == '1' or i == '0':
			c += 1
		else:
			return 0
		if c == len(str):
			return 1


def isBinary(str):
	flag = 0
	for i in str:
		if i == '1' or i == '0':
			flag = 1
		else:
			flag = 0
			break
	if flag == 1:
		return 1
	else:
		return 0


def isBinary(str):
	res = 1
	for i in str:
		if i != '1' and i != '0':
			return 0
	return res


def isBinary(str):
	for i in str:
		if int(i) > 1 or int(i) < 0:
			return 0
	return 1


def isBinary(str):
	s = '01'
	c = 0
	for i in str:
		if i not in s:
			c = 1
			break
	if c == 1:
		return False
	else:
		return True


def isBinary(str):
	x = list(str)
	for digit in x:
		if int(digit) == 1 or int(digit) == 0:
			continue
		else:
			return False
	return True


def isBinary(s):
	for i in range(0, len(s)):
		if s[i] != '0' and s[i] != '1':
			return 0
	return 1


def isBinary(str):
	for char in str:
		if char != '0' and char != '1':
			return 0
	return 1


def isBinary(str):
	for s in str:
		if s != '1':
			if s != '0':
				return False
	return True


def isBinary(str):
	s = {'0', '1'}
	i = set(str)
	if s == i or i == {'0'} or i == {'1'}:
		return 1
	return 0


def isBinary(str):
	for char in str:
		if char != '0' and char != '1':
			return False
	return True


def isBinary(str):
	count = 0
	for result in str:
		if result == '0' or result == '1':
			count += 1
		else:
			return 0
	if count == len(str):
		return 1


def isBinary(str):
	l = []
	for i in str:
		l.append(i)
	if len(l) == 1:
		if '1' in l or '0' in l:
			return True
	if '1' in l and '0' in l:
		return True
	else:
		return False


def isBinary(str):
	count = 0
	for s in str:
		if s == '1' or s == '0':
			count += 1
	if count == len(str):
		return True
	else:
		return False


def isBinary(str):
	for i in str:
		i = int(i)
		if i != 0 and i != 1:
			return 0
	return 1


def isBinary(str):
	for i in str:
		str1 = int(i)
		if str1 == 0 or str1 == 1:
			a = 1
			continue
		else:
			a = 0
			break
	return a


def isBinary(str):
	l = len(str)
	num = 0
	for e in str:
		if e == '0' or e == '1':
			num = num + 1
	res = 0
	if num == l:
		res = 1
	return res


def isBinary(str):
	if str.count('0') + str.count('1') == len(str):
		return 1
	else:
		return 0


def isBinary(str):
	s = str.replace('0', '')
	s = s.replace('1', '')
	if len(s) == 0:
		return 1
	return 0


def isBinary(str):
	for i in str:
		if i == '0' or i == '1':
			continue
		else:
			return 0
	return 1


def isBinary(str):
	l = len(str)
	for i in range(l):
		if str[i] == '0' or str[i] == '1':
			continue
		else:
			return 0
	return 1


def isBinary(str):
	result = []
	for i in range(0, len(str)):
		if str[i] == '0' or str[i] == '1':
			result.append(str[i])
	if len(result) == len(str):
		return 1
	else:
		return 0


def isBinary(str):
	k = ''
	for i in str:
		if i == '1' or i == '0':
			k += i
	if len(k) == len(str):
		return True
	else:
		return False


def isBinary(string):
	b = []
	count = 0
	for i in string:
		b.append(i)
	for i in range(len(b)):
		if b[i] == '1' or b[i] == '0':
			count += 1
	if count == len(b):
		return 1
	return 0


def isBinary(str):
	bi = '10'
	a = 0
	for i in str:
		if i not in bi:
			a = 1
	if a == 0:
		return 1
	else:
		return 0


def isBinary(str):
	cnt = 0
	for i in str:
		if i == '0' or i == '1':
			cnt += 1
	if cnt == len(str):
		return True
	return False


def isBinary(str):
	if set(str) == {'0', '1'} or set(str) == {'0'} or set(str) == {'1'}:
		return True
	else:
		return False


def isBinary(str):
	fl = False
	for i in str:
		if i != '0' and i != '1':
			fl = True
			break
	if fl:
		return 0
	return 1


def isBinary(str):
	try:
		i = int(str, 2)
	except:
		return 0
	return 1


def isBinary(str):
	flag = 1
	for i in str:
		if i not in ['0', '1']:
			flag = 0
	return flag


def isBinary(str1):
	for c in str1:
		if c != '0' and c != '1':
			return False
	return True


def isBinary(t):
	s = set(t)
	if len(s) > 2:
		return 0
	if '1' in s:
		s.remove('1')
	if '0' in s:
		s.remove('0')
	if len(s) == 0:
		return 1
	return 0


def isBinary(str):
	str = str.strip()
	s = 0
	for i in str:
		if i == '0' or i == '1':
			s += 1
	if s == len(str):
		return True
	else:
		return False


def isBinary(str):
	str_list = list(str)
	for i in range(len(str_list)):
		if not str_list[i] == '0' and (not str_list[i] == '1'):
			return 0
	return 1


def isBinary(str):
	flag = 0
	for i in str:
		if i != '1' and i != '0':
			flag = 1
			return False
	return True


def isBinary(str):
	a = '01'
	count = 0
	for i in str:
		if i in a:
			count = 1
			pass
		else:
			count = 0
			break
	if count == 1:
		return 1
	else:
		return 0


def isBinary(str):
	ans = 1
	l1 = ['0', '1']
	for i in str:
		if i not in l1:
			ans = 0
	return ans


def isBinary(str):
	for i in range(0, len(str)):
		if str[i] != '0' and str[i] != '1':
			return False
	return True


def isBinary(str):
	for c in str:
		if c != '0' and c != '1':
			return 0
	return 1


def isBinary(str):
	s = set(str)
	for i in s:
		if i != '0' and i != '1':
			return False
	return True


def isBinary(str):
	l = ['0', '1']
	count = 0
	for i in str:
		if i in l:
			count += 1
	if len(str) == count:
		return 1
	else:
		return 0


def isBinary(str1):
	for w in str1:
		if w == '0' or w == '1':
			continue
		else:
			return False
	return True


def isBinary(str):
	flag = True
	for i in str:
		if i not in ['0', '1']:
			flag = False
	if flag == True:
		return 1
	else:
		return 0


def isBinary(str):
	return all((int(i) in [0, 1] for i in str))


def isBinary(str):
	n = len(str)
	for i in range(n):
		if str[i] != '0' and str[i] != '1':
			return 0
	return 1


def isBinary(str):
	a = len(str)
	ans = 0
	for i in str:
		if i == '0' or i == '1':
			continue
		else:
			ans = 1
	if ans == 0:
		return True
	else:
		return False


def isBinary(str):
	str1 = len(str)
	for i in range(str1):
		if str[i] == '0' or str[i] == '1':
			pass
		else:
			return 0
	return 1


def isBinary(str):
	for i in str:
		if i > '1':
			return 0
	return 1


def isBinary(str):
	b = ['0', '1']
	ctr = 0
	for i in range(len(str)):
		if str[i] not in b:
			ctr += 1
			break
	if ctr > 0:
		return 0
	else:
		return 1