Given a string str. The task is to check whether it is a binary string or not.
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.
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.