Given a string str, the task is to check whether the string is valid time in 24-hour format or not by using Regular Expression.
The valid time in the 24-hour format must satisfy the following conditions.
- It should start from 0-23 or 00-23.
- It should be followed by a ‘:'(colon).
- It should be followed by two digits from 00 to 59.
- It should not end with ‘am’, ‘pm’ or ‘AM’, ‘PM’.
Examples:
Input: str = “13:05”
Output: true
Explanation: The given string satisfies all the above mentioned conditions.
Input: str = “02:15”
Output: true
Explanation: The given string satisfies all the above mentioned conditions.
Input: str = “24:00”
Output: false
Explanation: The given string doesn’t be in the range of 0-23 or 00-23(i.e., hour is out of range), therefore it is not a valid time in 24-hour format.
Input: str = “10:60”
Output: false
Explanation: The given string doesn’t be in the range of 00 to 59(i.e., minute is out of range), therefore it is not a valid time in 24-hour format.
Input: str = “10:15 PM”
Output: false
Explanation: The given string end with ‘AM’ or ‘PM’, therefore it is not a valid time in 24-hour format.
Approach: This problem can be solved by using Regular Expressions.
- Get the string.
- Create a regular expression to check time in 24-hour format as mentioned below:
regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
Where:
- ( represents the start of the group.
- [01]?[0-9] represents the time that starts with 0-9, 1-9, 00-09, 10-19.
- | represents or.
- 2[0-3] represents the time starting with 20-23.
- ) represents the end of the group.
- : represents the time that should be followed by a colon(:).
- [0-5][0-9] represents the time followed by 00 to 59
- Match the given string with the regex, in Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regex, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidTime(string str)
{
const regex pattern( "([01]?[0-9]|2[0-3]):[0-5][0-9]" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "13:05" ;
cout << str1 + ": " << isValidTime(str1) << endl;
string str2 = "02:15" ;
cout << str2 + ": " << isValidTime(str2) << endl;
string str3 = "24:00" ;
cout << str3 + ": " << isValidTime(str3) << endl;
string str4 = "10:60" ;
cout << str4 + ": " << isValidTime(str4) << endl;
string str5 = "10:15 PM" ;
cout << str5 + ": " << isValidTime(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidTime(String time)
{
String regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]" ;
Pattern p = Pattern.compile(regex);
if (time == null ) {
return false ;
}
Matcher m = p.matcher(time);
return m.matches();
}
public static void main(String args[])
{
String str1 = "13:05" ;
System.out.println(str1 + ": "
+ isValidTime(str1));
String str2 = "02:15" ;
System.out.println(str2 + ": "
+ isValidTime(str2));
String str3 = "24:00" ;
System.out.println(str3 + ": "
+ isValidTime(str3));
String str4 = "10:60" ;
System.out.println(str4 + ": "
+ isValidTime(str4));
String str5 = "10:15 PM" ;
System.out.println(str5 + ": "
+ isValidTime(str5));
}
}
|
Python3
import re
def isValidTime(time):
regex = "^([01]?[0-9]|2[0-3]):[0-5][0-9]$" ;
p = re. compile (regex);
if (time = = "") :
return False ;
m = re.search(p, time);
if m is None :
return False ;
else :
return True
if __name__ = = "__main__" :
str1 = "13:05" ;
print (str1, ": " , isValidTime(str1));
str2 = "02:15" ;
print (str2, ": " , isValidTime(str2));
str3 = "24:00" ;
print (str3, ": " , isValidTime(str3));
str4 = "10:60" ;
print (str4, ": " , isValidTime(str4));
str5 = "10:15 PM" ;
print (str5, ": " , isValidTime(str5));
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "13:05" , "02:15" , "24:00" , "10:60" , "10:15 PM" };
foreach ( string s in str) {
Console.WriteLine( isValidTime(s) ? "true" : "false" );
}
Console.ReadKey(); }
public static bool isValidTime( string str)
{
string strRegex = @"^([01]?[0-9]|2[0-3]):[0-5][0-9]$" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return ( true );
else
return ( false );
}
}
|
Javascript
function isValidTime(str) {
let regex = new RegExp(/^([01]\d|2[0-3]):?([0-5]\d)$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "13:05" ;
console.log(isValidTime(str1));
let str2 = "02:15" ;
console.log(isValidTime(str2));
let str3 = "24:00" ;
console.log(isValidTime(str3));
let str4 = "10:60" ;
console.log(isValidTime(str4));
let str5 = "10:15 PM" ;
console.log(isValidTime(str5));
|
Output:
13:05: true
02:15: true
24:00: false
10:60: false
10:15 PM: false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)