53. Valid Time


Task

Check if the given string is a correct time representation of the 24-hour clock.

Example

  • For time = "13:58", the output should be
    validTime(time) = true;

  • For time = "25:51", the output should be
    validTime(time) = false;

  • For time = "02:76", the output should be
    validTime(time) = false.

Input/Output

  • [execution time limit]
    4 seconds (py3)

  • [input] string time
    A string representing time in HH:MM format. It is guaranteed that the first two characters, as well as the last two characters, are digits.

  • [output] boolean
    true if the given representation is correct, false otherwise.

My Solution

import re

def validTime(time):
    correct = re.match('([01][0-9]|2[0-3]):[0-5][0-9]', time)
    if correct:
        return True
    else:
        return False