Problem Statement
The binary numeral system (base 2 numerals) represents numeric values using two symbols: 0 and 1. Counting in binary is similar to counting in any other number system. If you want to increase a number by 1, try to increase its last digit by 1. If this fails, set the last digit to zero, and try to increase the previous digit, and so on, until you succeed.
For example, the decimal sequence:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
converted to binary looks as follows:
1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, ...
You are given a
Definition
- Class:
- BinaryIncrementation
- Method:
- plusOne
- Parameters:
- String
- Returns:
- String
- Method signature:
- String plusOne(String x)
- (be sure your method is public)
Constraints
- x will contain between 1 and 30 characters, inclusive.
- Each character in x will be a one ('1') or a zero ('0').
- The first character in x will be a one ('1').
Examples
"10011"
Returns: "10100"
"10011" is binary for 16+2+1 = 19. The next integer is 20 = 16+4, which is "10100" in binary.
"10000"
Returns: "10001"
"1111"
Returns: "10000"
Be careful not to miss the case when the result is a power of two.
"1"
Returns: "10"
"101010101010101010101010101010"
Returns: "101010101010101010101010101011"
"111111111111111111111111111111"
Returns: "1000000000000000000000000000000"
"10"
Returns: "11"
"11"
Returns: "100"
"100"
Returns: "101"
"101"
Returns: "110"
"110"
Returns: "111"
"111"
Returns: "1000"
"1000"
Returns: "1001"
"1001"
Returns: "1010"
"1010"
Returns: "1011"
"1011"
Returns: "1100"
"100101010101010000"
Returns: "100101010101010001"
"101111111111111111111111111111"
Returns: "110000000000000000000000000000"
"100000000000000000000000000000"
Returns: "100000000000000000000000000001"
"100000000000011000000000000001"
Returns: "100000000000011000000000000010"
"100001111110111111011111"
Returns: "100001111110111111100000"
"100001"
Returns: "100010"
"111111111111111111111111111111"
Returns: "1000000000000000000000000000000"
"1111"
Returns: "10000"
"100101"
Returns: "100110"
"10001"
Returns: "10010"
"11111"
Returns: "100000"
"10011"
Returns: "10100"
"1"
Returns: "10"