1174. Readable Duration

Easy1.5 points estimatedStringMaths

A fitness app stores workout lengths as a plain count of seconds, but its summary screen should show something a person can read at a glance. Given the integer seconds, split it into whole days (86400 seconds each), then hours, then minutes, with whatever remains as seconds.

Return the parts as "Xd Yh Zm Ws": each part is the number followed by its unit letter, parts are separated by single spaces, and every part whose value is zero is left out completely. For example 3725 seconds becomes "1h 2m 5s". When seconds is zero, return "0s".

Integer division and remainders give the answer in constant time and space.

Example 1

Input:
seconds = 3725
Output:
"1h 2m 5s"
Explanation:

3725 seconds is 1 hour, 2 minutes and 5 seconds; there are no whole days.

Example 2

Input:
seconds = 90000
Output:
"1d 1h"
Explanation:

90000 seconds is 1 day and 1 hour; the zero minutes and zero seconds are omitted.

Example 3

Input:
seconds = 0
Output:
"0s"
Explanation:

Zero seconds is written as 0s.

Constraints

0 ≤ seconds ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(1)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…