You are given a string date representing a Gregorian calendar date in the yyyy-mm-dd format.
date can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in year-month-day format.
Return the binary representation of date.
Example 1:
Input: date = "2080-02-29"
Output: "100000100000-10-11101"
Explanation:
100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.
Example 2:
Input: date = "1900-01-01"
Output: "11101101100-1-1"
Explanation:
11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.
Constraints:
date.length == 10date[4] == date[7] == '-', and all other date[i]'s are digits.date represents a valid Gregorian calendar date between Jan 1st, 1900 and Dec 31st, 2100 (both inclusive).Problem Overview: You receive a date string in the format yyyy-mm-dd. The task is to convert the year, month, and day into their binary representations and return them joined by hyphens in the same order. Leading zeros are not required in the binary output.
Approach 1: String Split and Manual Conversion (O(n) time, O(1) space)
The most direct solution splits the input string using the - delimiter, producing three components: year, month, and day. Convert each component from string to integer, then convert the integer to its binary representation using built-in base conversion or manual division by 2. Finally, concatenate the binary strings with - between them. The algorithm performs a small number of conversions and string operations, so the runtime is effectively constant for a fixed date format, though conceptually O(n) with respect to the input string length.
This approach relies on basic operations from string manipulation and number conversion from math. It is easy to read, portable across languages, and maps directly to typical standard library functions like split() and toBinaryString(). Most production solutions follow this pattern because it is clear and requires minimal code.
Approach 2: Regular Expression Replacement (O(n) time, O(1) space)
A more compact alternative uses a regular expression to match the numeric parts of the date and replace them with their binary equivalents during substitution. The regex pattern captures groups of digits, and the replacement function converts each matched substring to an integer and then to binary. Each match is processed independently while preserving the hyphen separators.
This approach still performs the same underlying operations—parsing numbers and converting them to base 2—but embeds the transformation inside a regex replacement pipeline. It works well in languages like JavaScript or Python where replacement callbacks are concise. While elegant, it may be slightly harder to read for beginners compared to the explicit split-and-convert method.
Recommended for interviews: The string split and manual conversion approach. Interviewers expect you to parse the date using split, convert each component to an integer, and generate binary output. It demonstrates solid fundamentals with string processing and simple number base conversion. Regex solutions are valid but usually unnecessary unless the interviewer specifically asks for pattern-based transformations.
This approach involves splitting the input date string into year, month, and day using the hyphen '-' as a delimiter. Once the components are isolated, each is converted from a string to an integer, then to its binary form. We use language-specific functions to convert integers to binary, strip leading zeros, and join the components back with hyphens.
We use sscanf to split the string into year, month, and day. The atoi function converts them into integers. The sprintf function is then used to format the binary strings using the %b format specifier.
Time Complexity: O(1) since the operations involved are constant-time even though string handling functions are used.
Space Complexity: O(1) relative to the fixed size we allocated for result storage.
This efficient technique leverages regular expressions to capture and transform each calendar component (year, month, day) from the original date string. It involves a two-step process: first identifying components and then calling the conversion function. The binary equivalent is aggregated in concise string formation.
Utilizing JavaScript's replace method facilitated by a regular expression /\d+/g enables capturing each numeral group. The captured number undergoes binary transformation with toString(2). Results consolidate automatically into a single expression.
JavaScript
Python
Time Complexity: O(1) as replacement iterations are constant per input date.
Space Complexity: O(1) reflecting the fixed allocation for storing outcomes at each step.
We first split the string date by -, then convert each part to its binary representation, and finally join these three parts with -.
The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the string date.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| String Split and Manual Conversion | Time Complexity: O(1) since the operations involved are constant-time even though string handling functions are used. |
| Regular Expression Replacement | Time Complexity: O(1) as replacement iterations are constant per input date. |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Split and Manual Conversion | O(n) | O(1) | Best general solution. Clear logic using split and integer-to-binary conversion. |
| Regular Expression Replacement | O(n) | O(1) | Useful when performing inline pattern-based replacements in languages with strong regex support. |
3280. Convert Date to Binary (Leetcode Easy) • Programming Live with Larry • 663 views views
Watch 7 more video solutions →Practice Convert Date to Binary with our built-in code editor and test cases.
Practice on FleetCode