Problem Description
Explanation:
To solve this problem, we need to convert the given Gregorian calendar date into its binary representation in the format of year-month-day. We can achieve this by first splitting the input date into year, month, and day components, converting each component into binary, and then joining them back in the required format.
- Parse the input date string to extract year, month, and day components.
- Convert each component into its binary representation without leading zeroes.
- Join the binary representations of year, month, and day with hyphens to form the final output.
Time Complexity:
The time complexity of this solution is O(1) since the input date format is fixed.
Space Complexity:
The space complexity is also O(1) as we are not using any additional space that grows with the input size.
:
Solutions
class Solution {
public String convertDateToBinary(String date) {
String[] parts = date.split("-");
int year = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int day = Integer.parseInt(parts[2]);
String binaryYear = Integer.toBinaryString(year);
String binaryMonth = Integer.toBinaryString(month);
String binaryDay = Integer.toBinaryString(day);
return binaryYear + "-" + binaryMonth + "-" + binaryDay;
}
}
Loading editor...