Skip to main content

Reformat Date - Solution & Explanation

EasyString11 min readAsked at: Oracle, Adobe, Expedia +1
Practice this problem

Problem Statement

Given a date string in the form Day Month Year, where:

  • Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}.
  • Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}.
  • Year is in the range [1900, 2100].

Convert the date string to the format YYYY-MM-DD, where:

  • YYYY denotes the 4 digit year.
  • MM denotes the 2 digit month.
  • DD denotes the 2 digit day.

 

Example 1:

Input: date = "20th Oct 2052"
Output: "2052-10-20"

Example 2:

Input: date = "6th Jun 1933"
Output: "1933-06-06"

Example 3:

Input: date = "26th May 1960"
Output: "1960-05-26"

 

Constraints:

  • The given dates are guaranteed to be valid, so no error handling is necessary.

Approach Overview

Problem Overview: You receive a human-readable date string like "20th Oct 2052". The goal is to convert it into ISO format "2052-10-20". The challenge is extracting the day while removing ordinal suffixes (st, nd, rd, th) and converting the month abbreviation into its numeric representation.

Approach 1: String Parsing and Dictionary Mapping (Time: O(n), Space: O(1))

The most direct solution splits the input string into three parts: day, month, and year. The day portion contains a numeric value followed by a suffix (for example "6th" or "21st"), so you extract the numeric characters and pad with a leading zero if needed. The month abbreviation (Jan, Feb, etc.) is converted using a fixed dictionary that maps each month to its two-digit number. The year already appears in the required format. Finally, concatenate the parts as year-month-day. This approach relies only on basic string operations such as splitting, substring extraction, and dictionary lookup.

The key insight is that the month set is small and fixed (12 values), so a constant-time mapping works best. Each part of the string is processed once, giving linear time relative to the input length. Because the dictionary size never changes, the space usage is constant.

Approach 2: Regular Expression and String Manipulation (Time: O(n), Space: O(n))

Another option uses regular expressions to extract structured components from the input. A regex pattern can capture the numeric day, ignore the ordinal suffix, and isolate the month abbreviation and year. After matching, convert the month abbreviation through the same lookup table used in the previous method. Format the output string using zero-padded day and month values.

This method is useful when input formats vary slightly or when you want cleaner extraction logic in languages with strong regex support. However, regex introduces extra overhead and temporary match objects, which increases memory usage compared to simple splitting. The time complexity remains linear because the pattern scans the input string once.

Recommended for interviews: The string parsing and dictionary mapping approach is the expected solution. It shows that you can reason about input structure and build a straightforward transformation with constant auxiliary memory. Regex solutions work, but interviewers typically prefer explicit parsing since it demonstrates stronger control over string manipulation and avoids unnecessary abstraction. If you explain both options, start with manual parsing to show clarity, then mention regex as an alternative implementation.

Approach 1: String Parsing and Dictionary Mapping

In this approach, we will parse the given date string to extract the day, month, and year. We will then use a dictionary to map the month abbreviations to their respective numeric representations. Finally, the result is formatted to 'YYYY-MM-DD'.

This C solution involves splitting the input string into its components using sscanf, mapping the month to its numeric value, and then formatting it to the desired date format using sprintf. A fixed-size array is used for returning the formatted date.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), as we simply parse a fixed-length string and perform constant time operations.
Space Complexity: O(1), for storing result and month mapping.

Try this approach in the editor →

Approach 2: Regular Expression and String Manipulation

Here, we utilize a regular expression to extract the day, month, and year separately. This approach eliminates multiple string operations by capturing groups when matching patterns directly.

This C solution uses regular expressions to match and extract parts directly from the date string, reducing manual parsing efforts.

Code

C

Python

Complexity

Time Complexity: O(1), as regex processing and substring extraction are linear in relation to string length.
Space Complexity: O(1), with fixed allocation for regex and output.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

PHP

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
String Parsing and Dictionary Mapping

Time Complexity: O(1), as we simply parse a fixed-length string and perform constant time operations.
Space Complexity: O(1), for storing result and month mapping.

Regular Expression and String Manipulation

Time Complexity: O(1), as regex processing and substring extraction are linear in relation to string length.
Space Complexity: O(1), with fixed allocation for regex and output.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Parsing and Dictionary MappingO(n)O(1)Best general solution. Clear logic, constant memory, and easy to implement in interviews.
Regular Expression and String ManipulationO(n)O(n)Useful when parsing structured text or when regex utilities simplify extraction.

Video Solution

reformat date | reformat date leetcode | leetcode 1507 • Naresh Gupta • 1,330 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Reformat Date easy or hard?
Reformat Date is categorized as an Easy problem. It focuses on string parsing, handling ordinal suffixes, and mapping month abbreviations, making it a good practice problem for beginners learning string manipulation.
Reformat Date Python/Java solution
Python and Java solutions usually split the string by spaces, extract the numeric part of the day, and map the month abbreviation using a dictionary or HashMap. After zero-padding the day and using the mapped month number, the result is formatted as YYYY-MM-DD.
How to solve Reformat Date in O(n)?
Split the date string into three tokens: day, month, and year. Strip the suffix from the day (st, nd, rd, th), convert the month abbreviation using a fixed map like {"Jan":"01"}, and pad the day to two digits if needed. Combine the parts as year-month-day. Each step scans or processes the string once, resulting in O(n) time.
What is the best approach for Reformat Date?
The best approach uses string parsing with a dictionary that maps month abbreviations to their numeric values. Split the input into day, month, and year, remove the ordinal suffix from the day, and format the output as YYYY-MM-DD. This solution runs in O(n) time and uses O(1) extra space.
Is Reformat Date asked at Google/Amazon/Meta?
Reformat Date is a common easy-level string manipulation problem that appears in coding interview preparation sets. Variations of date parsing and formatting are frequently used by companies like Amazon and Google to test basic string handling and attention to edge cases.
What data structure is used in Reformat Date?
A hash map (dictionary) is typically used to convert month abbreviations such as Jan, Feb, and Mar into numeric values like 01, 02, and 03. The rest of the solution relies on basic string operations like splitting and substring extraction.
What is the time complexity of Reformat Date?
The optimal solution runs in O(n) time where n is the length of the input string. Each character is processed at most once while splitting and extracting components. The month lookup uses a constant-size dictionary, so it does not affect complexity.

Ready to solve this problem?

Practice Reformat Date with our built-in code editor and test cases.

Practice on FleetCode