Check if the hour value is valid if not valid then no need to change minute value , since time will still remain in valid , to check hour value flag is set to 1.
if ( ! isValid ( ( char ) h1 , ( char ) h2 , str , 1 ) ) { continue ; }