LeetCode 3280 - Convert Date to Binary

The problem gives us a date string in the format yyyy-mm-dd. The year always has four digits, while the month and day always have two digits because of zero padding. We need to convert each of the three components, year, month, and day, into their binary representations.

LeetCode Problem 3280

Difficulty: 🟢 Easy
Topics: Math, String

Solution

LeetCode 3280, Convert Date to Binary

Problem Understanding

The problem gives us a date string in the format yyyy-mm-dd. The year always has four digits, while the month and day always have two digits because of zero padding.

We need to convert each of the three components, year, month, and day, into their binary representations. The important detail is that the binary strings must not contain leading zeroes. After converting each part independently, we join them back together using hyphens in the same order.

For example, the date "2080-02-29" contains:

  • Year = 2080
  • Month = 02
  • Day = 29

Their binary forms are:

  • 2080 -> 100000100000
  • 2 -> 10
  • 29 -> 11101

The final result becomes:

100000100000-10-11101

The constraints are extremely small. The input length is always exactly 10 characters, and the format is guaranteed to be valid. This means we do not need to validate dates, handle malformed input, or worry about performance bottlenecks.

The most important implementation detail is handling leading zeroes correctly. The month "02" and day "01" must first be interpreted as integers before converting to binary. Otherwise, treating them as strings directly could produce incorrect output.

Another subtle point is that binary conversion should not include prefixes like 0b. Many programming language utilities return binary strings with such prefixes, so we must remove them or avoid adding them in the first place.

Approaches

Brute Force Approach

A brute force approach would manually convert each decimal number into binary using repeated division by 2.

For each component:

  1. Parse the string into an integer.
  2. Repeatedly divide the number by 2.
  3. Record the remainder at each step.
  4. Reverse the collected bits to form the binary representation.
  5. Join the three binary strings with hyphens.

This approach is correct because repeated division by 2 is the standard mathematical method for binary conversion. Every remainder represents one binary digit from least significant to most significant.

Although this works perfectly fine for the given constraints, it is more verbose than necessary because modern programming languages already provide built in binary conversion utilities.

Optimal Approach

The optimal approach is to leverage built in binary conversion functions.

The key observation is that the problem only requires straightforward format transformation:

  1. Split the date string by '-'.
  2. Convert each substring into an integer.
  3. Convert the integer into binary.
  4. Remove any language specific binary prefix.
  5. Join the converted parts with hyphens.

This solution is both simpler and cleaner than manually implementing binary conversion logic.

Approach Time Complexity Space Complexity Notes
Brute Force O(log Y + log M + log D) O(log Y + log M + log D) Manually builds binary strings using repeated division
Optimal O(log Y + log M + log D) O(log Y + log M + log D) Uses built in binary conversion utilities

Since the numbers are very small, both approaches are effectively constant time in practice.

Algorithm Walkthrough

  1. Split the input string using '-' as the delimiter.

This separates the date into three components: year, month, and day. For example:

"2080-02-29"
->
["2080", "02", "29"]
  1. Convert each component into an integer.

This step removes any leading zeroes automatically. For example:

"02" -> 2
"01" -> 1
  1. Convert each integer into its binary representation.

In Python, bin(number) returns a string like "0b1010". We remove the "0b" prefix using slicing.

In Go, strconv.FormatInt(..., 2) directly returns the binary representation without extra prefixes. 4. Store the converted binary strings.

Each converted component is collected in order:

yearBinary-monthBinary-dayBinary
  1. Join the binary strings with hyphens.

This preserves the original date structure while replacing decimal values with binary values.

Why it works

The algorithm works because each date component is converted independently from decimal to binary while preserving order and separators. Integer parsing removes unnecessary leading zeroes automatically, and binary conversion correctly represents the numeric value in base 2. Since the problem guarantees valid input formatting, the transformation is always well defined.

Python Solution

class Solution:
    def convertDateToBinary(self, date: str) -> str:
        parts = date.split("-")
        
        binary_parts = []
        
        for part in parts:
            number = int(part)
            binary_representation = bin(number)[2:]
            binary_parts.append(binary_representation)
        
        return "-".join(binary_parts)

The implementation begins by splitting the date string into its three components. Each component is then converted into an integer, which automatically removes leading zeroes.

Next, bin(number) converts the integer into binary form. Since Python includes the "0b" prefix, slicing with [2:] removes it cleanly.

Each binary string is stored in binary_parts, and finally the list is joined using hyphens to produce the required output format.

The implementation directly mirrors the algorithm walkthrough and keeps the code concise and readable.

Go Solution

package main

import (
	"strconv"
	"strings"
)

func convertDateToBinary(date string) string {
	parts := strings.Split(date, "-")

	for i, part := range parts {
		number, _ := strconv.Atoi(part)
		parts[i] = strconv.FormatInt(int64(number), 2)
	}

	return strings.Join(parts, "-")
}

The Go implementation follows the same logic as the Python version.

strings.Split separates the date into components. strconv.Atoi converts each string into an integer, automatically removing leading zeroes. strconv.FormatInt(number, 2) converts the integer into binary format directly.

Unlike Python's bin() function, Go does not prepend "0b", so no extra slicing is necessary.

Since the input constraints guarantee valid numeric strings, ignoring the conversion error from Atoi is acceptable in a LeetCode setting.

Worked Examples

Example 1

Input:

date = "2080-02-29"

After splitting:

Step Value
parts ["2080", "02", "29"]

Convert each component:

Original Integer Binary
"2080" 2080 "100000100000"
"02" 2 "10"
"29" 29 "11101"

Join the results:

"100000100000-10-11101"

Final output:

"100000100000-10-11101"

Example 2

Input:

date = "1900-01-01"

After splitting:

Step Value
parts ["1900", "01", "01"]

Convert each component:

Original Integer Binary
"1900" 1900 "11101101100"
"01" 1 "1"
"01" 1 "1"

Join the results:

"11101101100-1-1"

Final output:

"11101101100-1-1"

Complexity Analysis

Measure Complexity Explanation
Time O(log Y + log M + log D) Binary conversion depends on the number of bits in each number
Space O(log Y + log M + log D) Space is used to store the resulting binary strings

Because the year, month, and day values are bounded by very small constants, the runtime is effectively constant in practice. Even the largest possible year, 2100, requires only a small number of binary digits.

Test Cases

solution = Solution()

# Provided examples
assert solution.convertDateToBinary("2080-02-29") == "100000100000-10-11101"  # leap day example
assert solution.convertDateToBinary("1900-01-01") == "11101101100-1-1"  # minimum style date

# Boundary years
assert solution.convertDateToBinary("1900-12-31") == "11101101100-1100-11111"  # lower bound year
assert solution.convertDateToBinary("2100-12-31") == "100000110100-1100-11111"  # upper bound year

# Leading zero handling
assert solution.convertDateToBinary("2001-01-09") == "11111010001-1-1001"  # month/day with leading zeroes

# Small values
assert solution.convertDateToBinary("2000-02-02") == "11111010000-10-10"  # repeated small values

# Maximum month and day
assert solution.convertDateToBinary("2024-12-31") == "11111101000-1100-11111"  # largest month/day values

# Leap year
assert solution.convertDateToBinary("2024-02-29") == "11111101000-10-11101"  # valid leap date
Test Why
"2080-02-29" Validates provided example and leap day handling
"1900-01-01" Validates smallest style month/day values
"1900-12-31" Tests lower year boundary
"2100-12-31" Tests upper year boundary
"2001-01-09" Ensures leading zeroes are removed correctly
"2000-02-02" Tests repeated small binary values
"2024-12-31" Tests maximum month and day
"2024-02-29" Confirms leap year dates work correctly

Edge Cases

One important edge case is dates where the month or day contains leading zeroes, such as "2001-01-09". A naive implementation might incorrectly preserve the zeroes or attempt string based binary conversion. By converting each component to an integer first, the implementation naturally removes leading zeroes before binary conversion.

Another important case is the smallest possible month and day values, such as "01". Binary conversion of 1 should produce "1", not "01" or "001". The implementation handles this automatically because standard binary conversion functions never add leading zeroes.

A third important edge case is the upper constraint boundary, such as "2100-12-31". Although the numbers are still small, testing maximum valid values ensures there are no formatting issues or overflow concerns. Both Python and Go comfortably handle these integer ranges.

A final subtle case is leap day input like "2024-02-29". The problem guarantees valid Gregorian dates, so the implementation does not need to validate whether February 29 is legal for a particular year. The algorithm simply transforms the numeric components into binary form.