Problem

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.

Examples

Example 1

1
2
3
4
5
6
7
8
9

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

1
2
3
4
5
6
7
8
9

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

Solution

Method 1 – String Split and Binary Conversion

Intuition

To convert a date string in yyyy-mm-dd format to its binary representation, we split the string into year, month, and day, convert each to an integer, then to binary (without leading zeros), and join them with dashes.

Approach

  1. Split the input string by - to get year, month, and day.
  2. Convert each part to an integer.
  3. Convert each integer to its binary representation (without leading zeros).
  4. Join the binary strings with dashes in the order year-month-day.
  5. Return the result.

Code

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    string dateToBinary(string date) {
        int y = stoi(date.substr(0,4));
        int m = stoi(date.substr(5,2));
        int d = stoi(date.substr(8,2));
        return bitset<16>(y).to_string().substr(bitset<16>(y).to_string().find('1')) + "-" +
               bitset<8>(m).to_string().substr(bitset<8>(m).to_string().find('1')) + "-" +
               bitset<8>(d).to_string().substr(bitset<8>(d).to_string().find('1'));
    }
};
Go
1
2
3
4
5
6
func DateToBinary(date string) string {
    y, _ := strconv.Atoi(date[:4])
    m, _ := strconv.Atoi(date[5:7])
    d, _ := strconv.Atoi(date[8:])
    return strconv.FormatInt(int64(y), 2) + "-" + strconv.FormatInt(int64(m), 2) + "-" + strconv.FormatInt(int64(d), 2)
}
Java
1
2
3
4
5
6
7
8
class Solution {
    public String dateToBinary(String date) {
        int y = Integer.parseInt(date.substring(0, 4));
        int m = Integer.parseInt(date.substring(5, 7));
        int d = Integer.parseInt(date.substring(8));
        return Integer.toBinaryString(y) + "-" + Integer.toBinaryString(m) + "-" + Integer.toBinaryString(d);
    }
}
Kotlin
1
2
3
4
5
6
7
8
class Solution {
    fun dateToBinary(date: String): String {
        val y = date.substring(0, 4).toInt()
        val m = date.substring(5, 7).toInt()
        val d = date.substring(8).toInt()
        return Integer.toBinaryString(y) + "-" + Integer.toBinaryString(m) + "-" + Integer.toBinaryString(d)
    }
}
Python
1
2
3
4
class Solution:
    def dateToBinary(self, date: str) -> str:
        y, m, d = map(int, date.split('-'))
        return f"{bin(y)[2:]}-{bin(m)[2:]}-{bin(d)[2:]}"
Rust
1
2
3
4
5
6
impl Solution {
    pub fn date_to_binary(date: String) -> String {
        let parts: Vec<i32> = date.split('-').map(|x| x.parse().unwrap()).collect();
        format!("{:b}-{:b}-{:b}", parts[0], parts[1], parts[2])
    }
}
TypeScript
1
2
3
4
5
6
class Solution {
    dateToBinary(date: string): string {
        const [y, m, d] = date.split('-').map(Number);
        return y.toString(2) + '-' + m.toString(2) + '-' + d.toString(2);
    }
}

Complexity

  • ⏰ Time complexity: O(1), since the date string is always of fixed length.
  • 🧺 Space complexity: O(1), only a few variables are used.