Date Range Generator — LeetCode 2777 Python Solution

MediumLeetCode PremiumJavaScript
Problem
#2777
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

Python
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

MeasureComplexity
TimeO(k), where k is the number of yielded dates
SpaceO(1) auxiliary

Related problems

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.

Get Stealth Interview