{"id":8020,"date":"2021-01-23T21:37:38","date_gmt":"2021-01-24T05:37:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8020"},"modified":"2021-01-24T00:04:19","modified_gmt":"2021-01-24T08:04:19","slug":"leetcode-1736-latest-time-by-replacing-hidden-digits","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1736-latest-time-by-replacing-hidden-digits\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1736. Latest Time by Replacing Hidden Digits"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>time<\/code>&nbsp;in the form of&nbsp;<code>hh:mm<\/code>, where some of the digits in the string are hidden (represented by&nbsp;<code>?<\/code>).<\/p>\n\n\n\n<p>The valid times are those inclusively between&nbsp;<code>00:00<\/code>&nbsp;and&nbsp;<code>23:59<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the latest valid time you can get from<\/em>&nbsp;<code>time<\/code><em>&nbsp;by replacing the hidden<\/em>&nbsp;<em>digits<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> time = \"2?:?0\"\n<strong>Output:<\/strong> \"23:50\"\n<strong>Explanation:<\/strong> The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> time = \"0?:3?\"\n<strong>Output:<\/strong> \"09:39\"\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> time = \"1?:22\"\n<strong>Output:<\/strong> \"19:22\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>time<\/code>&nbsp;is in the format&nbsp;<code>hh:mm<\/code>.<\/li><li>It is guaranteed that you can produce a valid time from the given string.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Enumerate all possible clock in reverse order and find the first matching one.<\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  string maximumTime(string time) {\n    for (int m = 60 * 24 - 1; m >= 0; --m) {\n      int hh = m \/ 60;\n      int mm = m % 60;\n      if (time[0] != '?' && time[0] - '0' != hh \/ 10) continue;\n      if (time[1] != '?' && time[1] - '0' != hh % 10) continue;\n      if (time[3] != '?' && time[3] - '0' != mm \/ 10) continue;\n      if (time[4] != '?' && time[4] - '0' != mm % 10) continue;\n      time[0] = (hh \/ 10) + '0';\n      time[1] = (hh % 10) + '0';\n      time[3] = (mm \/ 10) + '0';\n      time[4] = (mm % 10) + '0';\n      break;\n    }\n    return time;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Rules<\/strong><\/h2>\n\n\n\n<p>Using rules, fill from left to right.<\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string maximumTime(string time) {\n    if (time[0] == '?') time[0] = time[1] >= '4' && time[1] <= '9' ? '1' : '2';\n    if (time[1] == '?') time[1] = time[0] == '2' ? '3' : '9';\n    if (time[3] == '?') time[3] = '5';\n    if (time[4] == '?') time[4] = '9';\n    return time;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;time&nbsp;in the form of&nbsp;hh:mm, where some of the digits in the string are hidden (represented by&nbsp;?). The valid times are those&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[108,222,4],"class_list":["post-8020","post","type-post","status-publish","format-standard","hentry","category-string","tag-clock","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8020"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8020\/revisions"}],"predecessor-version":[{"id":8042,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8020\/revisions\/8042"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}