1's Complement of a Binary String
00:00
The 1's complement of a binary number is obtained by inverting each of its bits: all 0s become 1s, and all 1s become 0s.
For example:
- The 1's complement of
101101is010010. - The 1's complement of
000is111.
Implement a function onesComplement(binaryString) that takes a string representing a binary number and returns its 1's complement as a string.
Important:
- If the input
binaryStringis empty or contains any character other than'0'or'1', your function should return an empty string""to indicate invalid input.
Constraints:
- For valid inputs,
1 <= binaryString.length <= 100 - The
binaryStringmay contain characters other than'0'or'1'for invalid test cases.
Examples
Input → "101101"
Output → "010010"
Input → "0000"
Output → "1111"
Input → "1111"
Output → "0000"