Skip to main content

Convert Date to Binary - Solution & Explanation

EasyMathString9 min readAsked at: Google
Practice this problem

Problem Statement

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 == 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 1st, 1900 and Dec 31st, 2100 (both inclusive).

Approach Overview

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 1: String Split and Manual Conversion

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Regular Expression Replacement

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.

Code

JavaScript

Python

Complexity

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.

Try this approach in the editor →

Approach 3: Simulation

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
String Split and Manual Conversion

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.

Regular Expression Replacement

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.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Split and Manual ConversionO(n)O(1)Best general solution. Clear logic using split and integer-to-binary conversion.
Regular Expression ReplacementO(n)O(1)Useful when performing inline pattern-based replacements in languages with strong regex support.

Video Solution

Convert Date to Binary || LeetCode Weekly Contest 414 || Leetcode Solution • codi • 895 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Convert Date to Binary easy or hard?
Convert Date to Binary is categorized as an Easy problem. The challenge mainly tests basic string parsing and number base conversion rather than complex algorithms or data structures.
Convert Date to Binary Python/Java solution
In Python, split the string using date.split('-'), convert each part with int(), and use bin(num)[2:] to get the binary string. In Java, use split("-") and Integer.toBinaryString(Integer.parseInt(part)) for each component before joining them with hyphens.
How to solve Convert Date to Binary in O(n)?
Split the date string using the '-' delimiter to extract year, month, and day. Convert each component to an integer and then to its binary representation using built-in base conversion. Join the binary values back together with hyphens to produce the final string.
What is the best approach for Convert Date to Binary?
The most practical approach splits the input string by '-' and converts each part (year, month, day) to binary individually. This method is simple, readable, and runs in O(n) time relative to the input string length with O(1) extra space.
Is Convert Date to Binary asked at Google/Amazon/Meta?
Problems like Convert Date to Binary appear in interview preparation platforms as easy warm-up questions. While not commonly reported in major company interview loops, similar tasks involving string parsing and base conversion are frequently used in coding screens.
What data structure is used in Convert Date to Binary?
No advanced data structures are required. The solution mainly uses basic string operations such as splitting and concatenation, along with integer conversion and binary formatting from standard libraries.
What is the time complexity of Convert Date to Binary?
The time complexity is O(n), where n is the length of the input string. Only a few operations are performed: splitting the string and converting three numbers to binary. Because the date format is fixed, the runtime behaves like constant time in practice.

Ready to solve this problem?

Practice Convert Date to Binary with our built-in code editor and test cases.

Practice on FleetCode