Check for Binary
Practice this coding question on Coding Gurukul.
Problem statement
A sensor controller accepts a status code only when every character is a binary digit. Before interpreting a submitted code, its validator checks the entire text. Your task Determine whether the nonempty string contains only 0 and 1. Important details Treat the input as text so leading zeroes are preserved. A code made entirely of zeroes is valid, as is one made entirely of ones. Any other digit or letter makes the whole code invalid, even if it occurs only once. Do not convert the string to a number to perform this check. Worked example The code 00101 is valid and produces 1. The code 0102 contains 2 and produces 0.
Input format
One nonempty alphanumeric string s.
Output format
Print 1 if binary, otherwise 0.
Constraints
1 <= length(s) <= 100000; s contains English letters or decimal digits.
Sample input
00101
Sample output
1
Sign in to run or submit a solution in C++, Java or Python.