LeetCode Problem Workspace

Convert Date to Binary

Convert a given Gregorian date string to its binary format by transforming year, month, and day individually without leading zeros.

category

2

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Math plus String

bolt

Answer-first summary

Convert a given Gregorian date string to its binary format by transforming year, month, and day individually without leading zeros.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math plus String

Try AiBox Copilotarrow_forward

This problem requires converting the year, month, and day components of a given date string into their binary representations. Each part must be transformed separately and concatenated in the yyyy-mm-dd format without leading zeros. The challenge tests precise string manipulation alongside fundamental math conversion skills, making it ideal for practicing Math plus String pattern problems in interviews.

Problem Statement

You are provided with a string date in the format yyyy-mm-dd representing a valid Gregorian calendar date. Your task is to convert each component of the date — year, month, and day — into its binary equivalent without any leading zeros.

Return a string that combines these binary representations in the original order as year-month-day. For example, converting 2080-02-29 should yield 100000100000-10-11101 since 2080 in binary is 100000100000, 2 is 10, and 29 is 11101.

Examples

Example 1

Input: date = "2080-02-29"

Output: "100000100000-10-11101"

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"

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).

Solution Approach

Parse and Split the Date

Extract the year, month, and day from the input string by splitting on the '-' character. Ensure each component is converted to an integer to prepare for binary conversion.

Convert Each Component to Binary

Use a built-in function or manual conversion to transform each integer into its binary representation. Remove any leading zeros to meet the problem requirement.

Concatenate Binary Components

Combine the binary year, month, and day using '-' as a separator. Return the resulting string as the final binary date output.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(1) because the date string has a fixed length of 10 characters and each conversion is constant time. Space complexity is O(1) since only a few variables are used for splitting and storing binary results.

What Interviewers Usually Probe

  • Watch for handling leading zeros incorrectly when converting each date part to binary.
  • Verify that month and day conversions correctly handle single-digit values.
  • Ensure concatenation preserves the yyyy-mm-dd order after conversion.

Common Pitfalls or Variants

Common pitfalls

  • Including leading zeros in the binary representations which violates the output format.
  • Misinterpreting the '-' positions and corrupting the date order in the final string.
  • Using string conversion without casting to integer first, which can lead to incorrect binary values.

Follow-up variants

  • Convert a time string hh:mm:ss to binary format using the same approach for hours, minutes, and seconds.
  • Return the binary date but pad each component to a fixed length, testing understanding of optional formatting.
  • Convert a list of multiple date strings to binary in bulk, emphasizing iteration over parsing and conversion logic.

FAQ

How do I convert the year, month, and day to binary for Convert Date to Binary?

Parse each component as an integer and use a binary conversion method, then remove any leading zeros before concatenation.

Does the input date always follow yyyy-mm-dd format?

Yes, the constraints guarantee a valid date string in yyyy-mm-dd format, so parsing on '-' is safe.

Should leading zeros appear in the binary output?

No, the problem explicitly requires no leading zeros in any component of the binary date string.

Can I use built-in binary conversion functions?

Yes, using built-in functions is acceptable and recommended to reduce conversion errors and simplify code.

What is the main pattern tested in this problem?

This problem tests the Math plus String pattern by combining numeric conversion with string manipulation carefully.

terminal

Solution

Solution 1: Simulation

We first split the string $\textit{date}$ by `-`, then convert each part to its binary representation, and finally join these three parts with `-`.

1
2
3
class Solution:
    def convertDateToBinary(self, date: str) -> str:
        return "-".join(f"{int(s):b}" for s in date.split("-"))
Convert Date to Binary Solution: Math plus String | LeetCode #3280 Easy