Easy

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] 0.5 seconds (c)

  • [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.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool validTime(char *time)
{
	return strlen(time)==5 && 10*(time[0]-'0')+time[1]-'0'<24 && time[2]==':' && 10*(time[3]-'0')+time[4]-'0'<60;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Different Squares  (0) 2020.04.06
<Codesignal> sumUpNumbers  (0) 2020.04.06
<Codesignal> longestWord  (0) 2020.04.06
<Codesignal> deleteDigit  (0) 2020.04.06
<Codesignal> chessKnight  (0) 2020.04.06

+ Recent posts