TopCoder problem "TheTriangleBothDivs" used in SRM 457 (Division I Level One , Division II Level Two)



Problem Statement

    John and Brus have always been confused by time zones. Their recent promotions to Concorde pilot and copilot do not help. Having to constantly fly through the Bermuda Triangle does not help either. They have therefore purchased a special digital clock that displays the current time in one of 19 time zones, from GMT-9 to GMT+9. The clock displays time in the format "HH:mm GMTof" (quotes for clarity), where HH is the hour of the day (between 00 and 23, inclusive), mm is the number of minutes since the start of the hour (between 00 and 59, inclusive), and of is the offset from "Greenwich Mean Time" (time zone GMT+0) to the current time zone. If the offset is +4, for example, it means the displayed time is 4 hours ahead of the time in GMT+0, and if the offset is -4, it means the time is 4 hours behind the time in GMT+0. GMT+0 will always be displayed with an offset of +0, so the clock will never display it as GMT-0.



In the middle of a flight, John wanted to know what time it was, so he asked Brus to check the clock. To Brus' surprise, the Bermuda Triangle had caused the clock to malfunction, and some of the characters on the display were unrecognizable. You are given the clock's display as a String time, where '?' characters represent the unrecognizable characters. Assuming that all the recognizable characters are accurate, determine what time it is in time zone GMT+0. Return this time formatted as "HH:mm" (quotes for clarity). If there are multiple possible answers, return the one that comes earliest lexicographically. The constraints ensure that at least one result is always possible.
 

Definition

    
Class:TheTriangleBothDivs
Method:fix
Parameters:String
Returns:String
Method signature:String fix(String time)
(be sure your method is public)
    
 

Notes

-String s1 comes before String s2 lexicographically if s1 has a lexicographically smaller character at the first index where they differ.
 

Constraints

-time will contain exactly 11 characters.
-time will be formatted "xx:xx GMTsx" (quotes for clarity).
-Each x in time will be a digit ('0'-'9') or '?'.
-The s in time will be '-','+' or '?'.
-time will represent a valid clock display (as described in the statement) where zero or more of the digits or the '-' or '+' sign have been replaced by '?' characters.
 

Examples

0)
    
"17:45 GMT-4"
Returns: "21:45"
1)
    
"16:?? GMT??"
Returns: "00:00"
There are many possible times the clock could be displaying, for example: "16:00 GMT+8", "16:35 GMT-9", etc. It is possible to choose "00" minutes and the GMT-8 time zone to yield time "00:00" which is the lexicographically first result.
2)
    
"?1:34 GMT-9"
Returns: "06:34"
3)
    
"??:?? GMT??"
Returns: "00:00"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10696

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=14144&pm=10696

Writer:

vexorian

Testers:

PabloGilberto , ivan_metelsky , Chmel_Tolstiy

Problem categories:

Simple Search, Iteration, String Manipulation, String Parsing