Reformat Date
viaLeetCode
Problem Convert a date from "Day Month Year" (Day ∈ {"1st","2nd",…,"31st"}, Month ∈ {"Jan",…,"Dec"}, Year ∈ [1900, 2100]) to ISO "YYYY-MM-DD".
Input / Output
- Input: string like "20th Oct 2052".
- Output: "2052-10-20".
Constraints
- Input guaranteed valid — the exercise is clean parsing/formatting, not validation (but mention what validation you'd add).
Example
- "6th Jun 1933" → "1933-06-06"; "26th May 1960" → "1960-05-26".
Expected approach
- Split on spaces; day = leading digits of token 0 (strip the ordinal suffix), month via a name → number map, year as-is; zero-pad day and month to two digits. O(1). Clean-code points: a Map for months rather than if-chains, string formatting (%02d), and noting locale/validation concerns if this were production code.
asked …