Watch 8 video solutions for Convert Date to Binary, a easy level problem involving Math, String. This walkthrough by Programming Live with Larry has 663 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |