Given a binary string (containing 1’s and 0’s), write a program to rely the variety of 0’s in a given string such that the next circumstances maintain:
- 1’s and 0’s are in any order in a string
- Use of conditional statements like if, if..else, and change should not allowed
- Use of addition/subtraction shouldn’t be allowed
Examples:
Enter : S = “101101”
Output : 2Enter : S = “00101111000”
Output : 6
Advisable: Please attempt your method on {IDE} first, earlier than shifting on to the answer.
Method: The above downside might be solved with the under thought:
If counter and conditional statements should not allowed. We’re having choice of Error dealing with.
Steps concerned within the implementation of the method:
- Take a string and a static variable counter which preserve the rely of zero.
- Traverse by means of every character and convert it into an integer.
- Take this quantity and use it as a denominator of the quantity “0”.
- Use the exception dealing with technique attempt..catch in a loop such that each time we received the Arithmetic exception, the counter will increment.
- Print the counter worth in consequence.
Beneath is the implementation of the above method:
Java
|
Time Complexity: O(N)
Auxiliary Area: O(1)
Associated Articles: