All problems
0874EasyMathString

Day Number Within the Year

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1154Day of the Year

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A log stamp is given as date, a string in the form "YYYY-MM-DD".

Return which day of its own year that date is, counting the first of January as day 1.

A year has 366 days when it is a leap year and 365 otherwise. A year is a leap year when it divides by 4, except that a year dividing by 100 is not a leap year unless it also divides by 400.

Examples

Example 1

Input
date = "2020-12-31"
Output
366

The year 2020 was a leap year, so it held 366 days and its final day is day 366.

Example 2

Input
date = "2020-03-01"
Output
61

The year 2020 divides by 4 and not by 100, so February held 29 days, putting the first of March on day 61.

Example 3

Input
date = "1900-03-01"
Output
60

The year 1900 divides by 100 but not by 400, so it was not a leap year and February held 28 days.

Constraints

  • date.length == 10
  • date[4] and date[7] are both '-', and every other character is a digit
  • date names a real calendar date between the first of January 1900 and the last of December 2100

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def day_of_the_year(date: str) -> int:
Java
public int dayOfTheYear(String date)
September 7
Apply