Difficulty: Easy | Acceptance: 88.80% | Paid: No Topics: Math, String
You are given a string date in the format YYYY-MM-DD (for example, 2024-02-16).
Convert the date string to the format of binary representation and return the result.
The binary representation of a number is the representation of that number in base-2. For example, the binary representation of 5 is “101”, and the binary representation of 10 is “1010”.
- Examples
- Constraints
- Approach 1: Built-in Conversion
- Approach 2: Manual Binary Conversion
Examples
Example 1:
Input: date = "2080-02-10"
Output: "100000100000-10-1010"
Explanation:
2080 in binary is "100000100000".
02 in binary is "10".
10 in binary is "1010".
Example 2:
Input: date = "1900-01-01"
Output: "11101101100-1-1"
Explanation:
1900 in binary is "11101101100".
01 in binary is "1".
01 in binary is "1".
Constraints
- date.length == 10
- date[4] == date[7] == '-', and all other date[i]'s are digits.
- The input is generated such that date represents a valid Gregorian calendar date between Jan 1^st, 1900 and Dec 31^st, 2100 (both inclusive).
Approach 1: Built-in Conversion
Intuition Most modern programming languages provide built-in utilities to convert integers to binary strings. We can leverage these standard library functions to parse the date components and convert them directly.
Steps
- Split the input string by the delimiter ’-’ to separate year, month, and day.
- Parse the resulting substrings into integers.
- Use the language’s built-in method (e.g.,
bin(),Integer.toBinaryString(),toString(2)) to convert each integer to its binary string representation. - Join the three binary strings with ’-’ and return the result.
class Solution:
def convertDateToBinary(self, date: str) -> str:
y, m, d = date.split('-')
return bin(int(y))[2:] + '-' + bin(int(m))[2:] + '-' + bin(int(d))[2:]
Complexity
- Time: O(1) - The number of operations is constant and bounded by the maximum value of the year (2100), which has a fixed number of bits.
- Space: O(1) - The space used for the output string is constant in size relative to the input constraints.
- Notes: This approach is the most concise and readable for production code.
Approach 2: Manual Binary Conversion
Intuition We can implement the mathematical process of converting a decimal number to binary manually. This involves repeatedly dividing the number by 2 and recording the remainders.
Steps
- Parse the year, month, and day from the string.
- For each number, initialize an empty string to store the binary result.
- While the number is greater than 0:
- Prepend the remainder of the number divided by 2 (n % 2) to the result string.
- Update the number by integer division by 2 (n /= 2).
- Join the resulting binary strings with ’-‘.
class Solution:
def convertDateToBinary(self, date: str) -> str:
def to_bin(n):
if n == 0: return '0'
res = ''
while n > 0:
res = str(n % 2) + res
n //= 2
return res
y, m, d = date.split('-')
return to_bin(int(y)) + '-' + to_bin(int(m)) + '-' + to_bin(int(d))
Complexity
- Time: O(1) - The loop runs for log₂(n) iterations, where n is at most 2100. This is a constant upper bound.
- Space: O(1) - Excluding the output string, we use only a few variables for computation.
- Notes: This approach demonstrates the underlying algorithm and is useful if built-in functions are restricted.