Python 1185.py
class Solution:
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
return date(year, month, day).strftime("%A")
Difficulty
and has an acceptance rate of about 59% (100k+ submissions).
Typical patterns to solve this problem:
- Math
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a class Solution with a single method dayOfTheWeek. The method receives three integer parameters-day, month, and year-and returns the full name of the weekday corresponding to that date. Internally it leverages Python’s standard library datetime module to construct a date object and then formats it using strftime with the %A directive, which yields the weekday name in English.
Key Steps
Import and object creation
The method implicitly relies on the date class from the datetime module. When date(year, month, day) is called, the constructor validates the supplied values and creates an immutable date instance representing the calendar day.
date(year, month, day)
Formatting the result
The strftime method of the date object is invoked with the format string "%A". This directive instructs the formatter to produce the locale-dependent full weekday name (e.g., “Monday”, “Tuesday”).
date(year, month, day).strftime("%A")
Return value
The formatted string is returned directly as the output of dayOfTheWeek.
return date(year, month, day).strftime("%A")
Edge Cases Handled
The datetime.date constructor performs comprehensive validation of the input components. It automatically rejects invalid day-month combinations such as February 30 or April 31, raising a ValueError. Leap year handling is built into the module, so dates like February 29 on a leap year are accepted, while the same date on a non-leap year triggers an exception. The method therefore gracefully handles out-of-range values by delegating to the robust validation logic of the standard library.
Performance Optimizations
The implementation is concise and leverages highly optimized C-level code inside the datetime module. By constructing a date object and immediately calling strftime, the solution avoids any manual calculation of offsets, month lengths, or leap year rules. This direct use of the library eliminates overhead associated with iterative or lookup-based approaches, resulting in minimal Python-level processing and fast execution for each call. The absence of intermediate data structures further reduces memory pressure, allowing the method to operate efficiently even when invoked repeatedly.
ELI5 - Simplified Explanation
Imagine you have a calendar book that knows every secret about time. This code is like building a tiny robot helper that looks at that book. You give the robot three puzzle pieces: the day, the month, and the year. The robot picks up those pieces and runs over to the calendar book to find the exact page for that date. Once it finds the page, it reads the name of the day, like Monday or Tuesday, written right there on the paper. It is just like asking a grown-up what day of the week a birthday falls on and getting the answer immediately.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | |||||||||
| www | AlgoMonster | C++ | Python | Java | TS | |||||
| WalkCCC | C++ | Python | Java | |||||||
| leetcode.ca | C++ | Python | Java | Go | TS | |||||
| Progiez | C++ | Python | Java | |||||||
| github | BigEggStudy | C# | ||||||||
| black-shadows | C++ | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| everthis | JS | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| Garvit244 | Python | |||||||||
| halfrost | Go | |||||||||
| JoshCrozier | JS | |||||||||
| keineahnung2345 | C++ | |||||||||
| lkwq007 | Python | |||||||||
| lzl124631x | C++ | |||||||||
| muhammadfarhankt | C | |||||||||
| suxrobGM | C# | |||||||||
| varunu28 | Java | |||||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||||
| WalkCCC | C++, Python, Java | |||||||||
| leetcode.ca | C++, Go, Java, Python, TypeScript | |||||||||
| Progiez | C++, Java, Python | |||||||||
| BigEggStudy | C# | |||||||||
| black-shadows | C++, C++ | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| everthis | JavaScript | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| Garvit244 | Python | |||||||||
| halfrost | Go | |||||||||
| JoshCrozier | JavaScript | |||||||||
| keineahnung2345 | C++ | |||||||||
| lkwq007 | Python | |||||||||
| lzl124631x | C++ | |||||||||
| muhammadfarhankt | C | |||||||||
| suxrobGM | C# | |||||||||
| varunu28 | Java |
Videos
Solved On The Same Day
- problem 1154: Day of the Year
- problem 2864: Maximum Odd Binary Number
- problem 1011: Capacity To Ship Packages Within D Days
- problem 1441: Build an Array With Stack Operations
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality