3280. Convert Date to Binary LeetCode Solution
In this guide, you will get 3280. Convert Date to Binary LeetCode Solution with the best time and space complexity. The solution to Convert Date to Binary problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Convert Date to Binary solution in C++
- Convert Date to Binary solution in Java
- Convert Date to Binary solution in Python
- Additional Resources
Problem Statement of Convert Date to Binary
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).
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
3280. Convert Date to Binary LeetCode Solution in C++
class Solution {
public:
string convertDateToBinary(string date) {
const int year = stoi(date.substr(0, 4));
const int month = stoi(date.substr(5, 2));
const int day = stoi(date.substr(8, 2));
return toBinary(year) + '-' + toBinary(month) + '-' + toBinary(day);
}
private:
// Converts an integer to binary without leading zeros.
string toBinary(int value) {
const string binary = bitset<16>(value).to_string();
return binary.substr(binary.find('1')); // Remove leading zeros.
}
};
/* code provided by PROGIEZ */
3280. Convert Date to Binary LeetCode Solution in Java
class Solution {
public String convertDateToBinary(String date) {
final int year = Integer.parseInt(date.substring(0, 4));
final int month = Integer.parseInt(date.substring(5, 7));
final int day = Integer.parseInt(date.substring(8, 10));
StringBuilder sb = new StringBuilder();
sb.append(Integer.toBinaryString(year))
.append("-")
.append(Integer.toBinaryString(month))
.append("-")
.append(Integer.toBinaryString(day));
return sb.toString();
}
}
// code provided by PROGIEZ
3280. Convert Date to Binary LeetCode Solution in Python
class Solution:
def convertDateToBinary(self, date: str) -> str:
year, month, day = map(int, date.split('-'))
def toBinary(value: int) -> str:
"""Converts an integer to binary without leading zeros."""
return bin(value)[2:]
return '-'.join([toBinary(year), toBinary(month), toBinary(day)])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.