Problem Statement
We have a timer that counts elapsed hours and minutes.
You are given two
Both times are given in the format "hhH:MM" (quotes added for clarity). This means that:
- There are always exactly two digits for minutes.
- There are between one and three digits for hours.
Note that the number of hours in time1 and/or time2 may have unnecessary leading zeros. (See the Examples.)
You must return the answer in the format "[-]hhH:MM". More precisely:
- The answer must start with a minus if and only if the time difference is negative, i.e., if time2 was shown strictly sooner than time1.
- The rest of the answer must be the absolute value of the time difference, in hours and minutes.
- The number of minutes must always be given with exactly two digits.
- The number of hours must not have unnecessary leading zeros.
Definition
- Class:
- TimeDifference
- Method:
- calculate
- Parameters:
- String, String
- Returns:
- String
- Method signature:
- String calculate(String startTime, String endTime)
- (be sure your method is public)
Notes
- There are 60 minutes in an hour, so the number of minutes will always be between 00 and 59, inclusive.
Constraints
- time1 and time2 will be in the format described in the problem statement.
Examples
"00:00"
"13:23"
Returns: "13:23"
The timer started at "00:00" and after 13 hours and 23 minutes it showed "13:23".
"100:15"
"13:23"
Returns: "-86:52"
The timer showed "13:23" almost 87 hours earlier than "100:15".
"001:00"
"8:15"
Returns: "7:15"
Unnecessary leading zeroes are allowed in the input, but not in the output. The answer "07:15" would be rejected.
"123:45"
"543:21"
Returns: "419:36"
"111:11"
"222:12"
Returns: "111:01"
The number of minutes must always be printed with exactly two digits. The answer should not be "111:1".
"10:42"
"10:47"
Returns: "0:05"
If the number of hours in the difference is 0, the '0' still has to be printed as the number of hours. (This zero is not considered an unnecessary leading zero.)
"10:47"
"010:47"
Returns: "0:00"
Even though the strings are not equal, they do represent the same time, so the time difference is zero hours and zero minutes. (Note that the answer in this case should not start with a '-'.)
"08:27"
"464:36"
Returns: "456:09"
"32:32"
"50:39"
Returns: "18:07"
"6:42"
"1:00"
Returns: "-5:42"
"61:46"
"009:58"
Returns: "-51:48"
"72:27"
"3:33"
Returns: "-68:54"
"367:28"
"0:35"
Returns: "-366:53"
"91:44"
"76:21"
Returns: "-15:23"
"588:48"
"3:50"
Returns: "-584:58"
"936:59"
"2:21"
Returns: "-934:38"
"5:37"
"65:34"
Returns: "59:57"
"12:41"
"5:13"
Returns: "-7:28"
"95:07"
"77:22"
Returns: "-17:45"
"893:18"
"8:59"
Returns: "-884:19"
"45:34"
"041:30"
Returns: "-4:04"
"72:49"
"33:46"
Returns: "-39:03"
"818:14"
"116:40"
Returns: "-701:34"
"6:31"
"77:59"
Returns: "71:28"
"152:28"
"2:35"
Returns: "-149:53"
"823:48"
"1:17"
Returns: "-822:31"
"832:39"
"487:50"
Returns: "-344:49"
"46:58"
"9:16"
Returns: "-37:42"
"623:33"
"931:30"
Returns: "307:57"
"653:51"
"02:36"
Returns: "-651:15"
"3:22"
"88:11"
Returns: "84:49"
"7:58"
"2:06"
Returns: "-5:52"
"9:22"
"401:06"
Returns: "391:44"
"56:00"
"125:26"
Returns: "69:26"
"5:01"
"05:08"
Returns: "0:07"
"8:53"
"25:50"
Returns: "16:57"
"67:29"
"580:00"
Returns: "512:31"
"075:11"
"8:21"
Returns: "-66:50"
"887:02"
"240:45"
Returns: "-646:17"
"6:07"
"1:19"
Returns: "-4:48"
"8:19"
"9:57"
Returns: "1:38"
"547:09"
"593:48"
Returns: "46:39"
"79:54"
"082:36"
Returns: "2:42"
"43:35"
"1:26"
Returns: "-42:09"
"45:58"
"72:54"
Returns: "26:56"
"76:08"
"8:07"
Returns: "-68:01"
"83:50"
"02:27"
Returns: "-81:23"
"534:48"
"4:50"
Returns: "-529:58"
"501:32"
"3:22"
Returns: "-498:10"
"21:29"
"9:06"
Returns: "-12:23"
"470:29"
"81:34"
Returns: "-388:55"
"711:44"
"8:12"
Returns: "-703:32"
"293:02"
"69:30"
Returns: "-223:32"
"08:46"
"8:45"
Returns: "-0:01"
"613:30"
"762:50"
Returns: "149:20"
"74:19"
"98:43"
Returns: "24:24"
"1:06"
"828:19"
Returns: "827:13"
"42:34"
"13:52"
Returns: "-28:42"
"9:47"
"46:03"
Returns: "36:16"
"74:55"
"6:40"
Returns: "-68:15"
"32:13"
"26:29"
Returns: "-5:44"
"840:52"
"56:59"
Returns: "-783:53"
"40:55"
"056:09"
Returns: "15:14"
"491:00"
"6:55"
Returns: "-484:05"
"66:28"
"834:55"
Returns: "768:27"
"37:06"
"15:59"
Returns: "-21:07"
"6:21"
"54:15"
Returns: "47:54"
"583:32"
"40:35"
Returns: "-542:57"
"9:43"
"76:37"
Returns: "66:54"
"609:44"
"939:29"
Returns: "329:45"
"571:11"
"718:24"
Returns: "147:13"
"2:39"
"9:04"
Returns: "6:25"
"52:03"
"7:11"
Returns: "-44:52"
"407:00"
"142:47"
Returns: "-264:13"
"8:24"
"6:31"
Returns: "-1:53"
"466:40"
"93:07"
Returns: "-373:33"
"410:06"
"6:26"
Returns: "-403:40"
"106:54"
"179:57"
Returns: "73:03"
"510:30"
"76:53"
Returns: "-433:37"
"6:33"
"335:27"
Returns: "328:54"
"935:12"
"50:54"
Returns: "-884:18"
"2:45"
"42:41"
Returns: "39:56"
"107:16"
"11:22"
Returns: "-95:54"
"6:39"
"9:14"
Returns: "2:35"
"659:13"
"263:45"
Returns: "-395:28"
"64:38"
"8:03"
Returns: "-56:35"
"92:11"
"89:15"
Returns: "-2:56"
"78:30"
"9:55"
Returns: "-68:35"
"77:15"
"983:35"
Returns: "906:20"
"13:21"
"2:37"
Returns: "-10:44"
"3:54"
"1:09"
Returns: "-2:45"
"172:46"
"1:05"
Returns: "-171:41"
"9:25"
"15:26"
Returns: "6:01"
"70:40"
"5:49"
Returns: "-64:51"
"5:06"
"3:51"
Returns: "-1:15"
"138:21"
"5:49"
Returns: "-132:32"
"679:58"
"64:34"
Returns: "-615:24"
"770:49"
"922:22"
Returns: "151:33"
"531:27"
"449:27"
Returns: "-82:00"
"423:46"
"02:12"
Returns: "-421:34"
"06:38"
"12:28"
Returns: "5:50"
"127:06"
"55:47"
Returns: "-71:19"
"5:50"
"6:30"
Returns: "0:40"
"936:59"
"486:44"
Returns: "-450:15"
"122:59"
"3:40"
Returns: "-119:19"
"8:31"
"18:43"
Returns: "10:12"
"4:04"
"9:19"
Returns: "5:15"
"00:00"
"012:00"
Returns: "12:00"
"10:48"
"10:47"
Returns: "-0:01"
"11:12"
"001:01"
Returns: "-10:11"
"100:00"
"51:00"
Returns: "-49:00"
"10:11"
"10:10"
Returns: "-0:01"
"10:42"
"10:42"
Returns: "0:00"
"10:42"
"10:38"
Returns: "-0:04"