Python | Check if a given string is binary string or not
Last Updated :
03 May, 2023
Given string str. The task is to check whether it is a binary string or not.
Examples:
Input: str = "01010101010"
Output: Yes
Input: str = "geeks101"
Output: No
Approach 1: Using Set
- Insert the given string in a set
- Check if the set characters consist of 1 and/or 0 only.
Example:
Python3
def check(string):
p = set (string)
s = { '0' , '1' }
if s = = p or p = = { '0' } or p = = { '1' }:
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
string = "101010000111"
check(string)
|
Time Complexity: O(n), Here n is the length of the string.
Auxiliary Space: O(1), As constant extra space is used.
Approach 2: Simple Iteration
- Iterate for every character and check if the character is 0 or 1.
- If it is not then set a counter and break.
- After iteration check whether the counter is set or not.
Python3
def check2(string):
t = '01'
count = 0
for char in string:
if char not in t:
count = 1
break
else :
pass
if count:
print ( "No" )
else :
print ( "Yes" )
if __name__ = = "__main__" :
string = "001021010001010"
check2(string)
|
Time Complexity: O(n), Here n is the length of the string.
Auxiliary Space: O(1), As constant extra space is used.
- Compile a regular expression using compile() for “character is not 0 or 1”.
- Use re.findall() to fetch the strings satisfying the above regular expression.
- Print output based on the result.
Python3
import re
sampleInput = "1001010"
c = re. compile ( '[^01]' )
if ( len (c.findall(sampleInput))):
print ( "No" )
else :
print ( "Yes" )
|
Time complexity: O(n)
Auxiliary space: O(1)
Approach 4: Using exception handling and int
Python has a built in method to convert a string of a specific base to a decimal integer, using int(string, base). If the string passed as an argument is not of the specified base, then a ValueError is raised.
Python3
def check(string):
try :
int (string, 2 )
except ValueError:
return "No"
return "Yes"
if __name__ = = "__main__" :
string1 = "101011000111"
string2 = "201000001"
print (check(string1))
print (check(string2))
|
Time Complexity: O(1),
Auxiliary Space: O(1)
Approach 5 : Using count() function
Python3
string = "01010101010"
if (string.count( '0' ) + string.count( '1' ) = = len (string)):
print ( "Yes" )
else :
print ( "No" )
|
Time Complexity: O(n), where n is number of characters in string.
Auxiliary Space: O(1)
Approach 6 : Using replace() and len() methods
Python3
string = "01010121010"
binary = "01"
for i in binary:
string = string.replace(i,"")
if ( len (string) = = 0 ):
print ( "Yes" )
else :
print ( "No" )
|
Approach 7: Using all()
The all() method in Python3 can be used to evaluate if all the letters in the string are 0 or 1.
Python3
def check(string):
if all ((letter in "01" ) for letter in string):
return "Yes"
return "No"
if __name__ = = "__main__" :
string1 = "101011000111"
string2 = "201000001"
print (check(string1))
print (check(string2))
|
Time Complexity: O(n), Here n is the length of the string.
Auxiliary Space: O(1), As constant extra space is used.
Approach 8: Using issubset() method
We can use a issubset method in comprehension to check if all characters in the string are either 0 or 1.
Python3
def is_binary_string(s):
unique_chars = {c for c in s}
return unique_chars.issubset({ '0' , '1' })
if __name__ = = "__main__" :
string1 = "101011000111"
string2 = "201000001"
print (is_binary_string(string1))
print (is_binary_string(string2))
|
Time complexity: O(n), where n is the length of the input string, and
Auxiliary space: O(1).
Method: Using re.search()
- Import the re module.
- Define a regular expression that matches any character other than ‘0’ and ‘1’.
- Use the re.search() method to search for the regular expression in the given string.
- If a match is found, then the given string is not a binary string. Otherwise, it is.
Python3
import re
def is_binary_string( str ):
regex = r "[^01]"
if re.search(regex, str ):
return False
else :
return True
print (is_binary_string( "01010101010" ))
print (is_binary_string( "geeks101" ))
|
Time Complexity: O(n), where n is the length of the given string (as we are iterating over the string once).
Auxiliary Space: O(1)
Please Login to comment...