Check if number is Binary

Easy
0/40
Average time to solve is 10m
profile
Contributed by
2 upvotes
Asked in companies
VisaDisney + HotstarOracle

Problem statement

Given a string of integers ‘bin’. Return 'true' if the string represents a valid binary number, else return 'false'. A binary number is a number that has only 0 or 1 in it.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.

The first line of each testcase contains a single string ‘bin’.
Output Format:
For each test case, return a boolean value that is 'true' if the string is binary and 'false' if the string is not binary.

The output for each test case is printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= length(bin) <= 10^4

Where length(bin) is the length of the binary string.

Time Limit: 1 sec
Sample Input 1:
3
1000
20110
101013
Sample Output 1:
YES
NO 
NO
Explanation of Sample Input 1:
Test Case 1: 
We see that the given string is “1000”. We can clearly see that all of the string elements are either ‘1’ or ‘0’. Therefore we return true and print YES in a new line.

Test Case 2:
We see that the given string is “20110”. We can clearly see that the first string is not a binary string as the first element of the string is a ‘2’ which is not a binary number. Therefore we return false and print “NO” in a new line.

Test Case 3:
We see that the given string is “101013”. We can clearly see that the first string is not a binary string as the last element of the string is a ‘3’ which is not a binary number. Therefore we return false and print “NO” in a new line.
Sample Input 2:
5
1010111
91102
1010101
000001
122345
Sample Output 2:
YES
NO 
YES
YES 
NO
Hint

Try to find the answer recursively

Approaches (2)
Recursive Approach
  • The key idea is that for a string to be a valid binary number, each value in the string can either be a 1 or a 0. So we just need to check each element of the string and if we find any element which is not a 1 or a 0, we return false.
  • Otherwise, if we have traversed all the characters, and all of them are either ‘0’ or ‘1’ we return true.

 

From the above idea, we can devise the following approach:

 

  1. Call ‘isBinaryHelper ( ‘BIN‘, 0)’ function and store the value returned in a boolean variable ‘ans’.
  2. Return ‘ans’.

 

isBinaryHelper ( ‘BIN’ , ‘CUR_IDX’ ):

 

  1. Base Case: If ‘CUR_IDX’ is the last element of the array:
    • Return ‘true’
  2. If ‘BIN[CUR_IDX]’ is not ‘1’ or ‘0’, then:
    • Then we return ‘false’ immediately.
  3. Otherwise:
    • We recursively call the function for the next element.

 

NOTE: This approach will result in a ‘StackOverflow’ error in Java because of excessive memory usage in the recursion stack.

Time Complexity

O(N), where ‘N’ is the number of elements in the string.

 

We need to recursively check each element of the string. Therefore the time complexity is of the order of ‘N’.

Space Complexity

O(N), where ‘N’ is the number of elements in the binary string.

 

Recursive stack uses space of order of ‘N’.

Code Solution
(100% EXP penalty)
Check if number is Binary
Full screen
Console