{"id":7067,"date":"2020-07-11T20:58:04","date_gmt":"2020-07-12T03:58:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7067"},"modified":"2020-07-11T21:02:20","modified_gmt":"2020-07-12T04:02:20","slug":"leetcode-1507-reformat-date","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1507-reformat-date\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1507. Reformat Date"},"content":{"rendered":"\n<p>Given a&nbsp;<code>date<\/code>&nbsp;string in the form&nbsp;<code>Day Month Year<\/code>, where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>Day<\/code>&nbsp;is in the set&nbsp;<code>{\"1st\", \"2nd\", \"3rd\", \"4th\", ..., \"30th\", \"31st\"}<\/code>.<\/li><li><code>Month<\/code>&nbsp;is in the set&nbsp;<code>{\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"}<\/code>.<\/li><li><code>Year<\/code>&nbsp;is in the range&nbsp;<code>[1900, 2100]<\/code>.<\/li><\/ul>\n\n\n\n<p>Convert the date string to the format&nbsp;<code>YYYY-MM-DD<\/code>, where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>YYYY<\/code>&nbsp;denotes the 4 digit year.<\/li><li><code>MM<\/code>&nbsp;denotes the 2 digit month.<\/li><li><code>DD<\/code>&nbsp;denotes the 2 digit day.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> date = \"20th Oct 2052\"\n<strong>Output:<\/strong> \"2052-10-20\"\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> date = \"6th Jun 1933\"\n<strong>Output:<\/strong> \"1933-06-06\"\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> date = \"26th May 1960\"\n<strong>Output:<\/strong> \"1960-05-26\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The given dates are guaranteed to be valid, so no error handling is necessary.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: String + HashTable<\/strong><\/h2>\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 reformatDate(string date) {\n    stringstream ss(date);\n    string day, month, year;\n    ss >> day >> month >> year;\n    unordered_map<string, string> m{{\"Jan\", \"01\"},\n                                    {\"Feb\", \"02\"},\n                                    {\"Mar\", \"03\"},\n                                    {\"Apr\", \"04\"},\n                                    {\"May\", \"05\"},\n                                    {\"Jun\", \"06\"},\n                                    {\"Jul\", \"07\"},\n                                    {\"Aug\", \"08\"},\n                                    {\"Sep\", \"09\"},\n                                    {\"Oct\", \"10\"},\n                                    {\"Nov\", \"11\"},\n                                    {\"Dec\", \"12\"}};\n    day = day.substr(0, day.length() - 2);\n    if (day.length() == 1) day = \"0\" + day;\n    return year + \"-\" + m[month] + \"-\" + day;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\n\/\/ Author: Huahua\nclass Solution {\n  public String reformatDate(String date) {\n    Map<String, String> m = new HashMap<String, String>();\n    m.put(\"Jan\", \"01\");\n    m.put(\"Feb\", \"02\");\n    m.put(\"Mar\", \"03\");\n    m.put(\"Apr\", \"04\");\n    m.put(\"May\", \"05\");\n    m.put(\"Jun\", \"06\");\n    m.put(\"Jul\", \"07\");\n    m.put(\"Aug\", \"08\");\n    m.put(\"Sep\", \"09\");\n    m.put(\"Oct\", \"10\");\n    m.put(\"Nov\", \"11\");\n    m.put(\"Dec\", \"12\");\n    String[] items = date.split(\" \");    \n    String day = items[0].substring(0, items[0].length() - 2);\n    if (day.length() == 1) day = \"0\" + day;\n    return items[2] + \"-\" + m.get(items[1]) + \"-\" + day;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n# Author: Huahua\nclass Solution:\n  def reformatDate(self, date: str) -> str:\n    m = {\"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \n         \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\", \n         \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \n         \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"}\n    items = date.split(\" \")\n    day = items[0][:-2]\n    if len(day) == 1: day = \"0\" + day\n    return items[2] + \"-\" + m[items[1]] + \"-\" + day\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;date&nbsp;string in the form&nbsp;Day Month Year, where: Day&nbsp;is in the set&nbsp;{&#8220;1st&#8221;, &#8220;2nd&#8221;, &#8220;3rd&#8221;, &#8220;4th&#8221;, &#8230;, &#8220;30th&#8221;, &#8220;31st&#8221;}. Month&nbsp;is in the set&nbsp;{&#8220;Jan&#8221;, &#8220;Feb&#8221;, &#8220;Mar&#8221;, &#8220;Apr&#8221;,&#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":[495,82,276,4],"class_list":["post-7067","post","type-post","status-publish","format-standard","hentry","category-string","tag-date","tag-hashtable","tag-split","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7067","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=7067"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7067\/revisions"}],"predecessor-version":[{"id":7070,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7067\/revisions\/7070"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}