MediumNo. 2777LeetCode Premium

Date Range Generator — LeetCode 2777 Python Solution

Topics
JavaScript
Reading time
2 min

The problem

Given a start date start, an end date end, and a positive integer step, return a generator object that yields dates in the range from start to end inclusive. The value of step indicates the number of days between consecutive yielded values.

Example

Input
start = "2023-04-01", end = "2023-04-04", step = 1
Output
["2023-04-01","2023-04-02","2023-04-03","2023-04-04"]
Explanation
const g = dateRangeGenerator(start, end, step);

Python solution

PythonSolution
from datetime import date, timedelta


def dateRangeGenerator(start: str, end: str, step: int):
    cur = date.fromisoformat(start)
    last = date.fromisoformat(end)
    delta = timedelta(days=step)
    while cur <= last:
        yield cur.isoformat()
        cur += delta

Complexity

Time
O(k)where k is the number of yielded dates
Space · auxiliary
O(1)

Related problems

Related problems
No.ProblemDifficulty
2778Sum of Squares of Special ElementsEasy
0057Insert IntervalMedium
0192Word FrequencyMedium
0193Valid Phone NumbersEasy
0194Transpose FileMedium
0195Tenth LineEasy

Frequently asked questions

How hard is LeetCode 2777. Date Range Generator?

LeetCode 2777. Date Range Generator is rated Medium on LeetCode.

What is the time complexity of LeetCode 2777. Date Range Generator?

The Python solution on this page runs in O(k), where k is the number of yielded dates.

What is the space complexity of LeetCode 2777. Date Range Generator?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 2777. Date Range Generator cover?

LeetCode 2777. Date Range Generator is tagged JavaScript on LeetCode.

Is LeetCode 2777. Date Range Generator a premium problem?

Yes. LeetCode 2777. Date Range Generator is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.