Google | Phone Screen | Valid Dates
Anonymous User
1987
Oct 09, 2019
Oct 13, 2019

Is there a optimal approach for it?

Question
Given any 3 positive integers, return all the distinct and valid dates the 3 integers can form.
The format of valid date is [Year, Month, Day] and the rule for a leap year Y is:

  • (Y % 4 == 0 and Y % 100 != 0) or (Y % 400 == 0 and Y % 3200 != 0)

Example 1:

Input: [2, 5, 29] 
Output: [[2, 5, 29], [29, 2, 5], [29, 5, 2]

Example 2:

Input: [100, 28, 15] 
Output: []
Explanation: 
No month can be greater than 12.

Example 3:

Input: [2, 2016, 29]
Output: [[2016, 2, 29]]
Explanation:
C.E 2016 is a leap year.
Comments (8)