{"id":7820,"date":"2020-12-20T01:03:59","date_gmt":"2020-12-20T09:03:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7820"},"modified":"2020-12-20T01:06:59","modified_gmt":"2020-12-20T09:06:59","slug":"leetcode-1694-reformat-phone-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1694-reformat-phone-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1694. Reformat Phone Number"},"content":{"rendered":"\n<p>You are given a phone number as a string&nbsp;<code>number<\/code>.&nbsp;<code>number<\/code>&nbsp;consists of digits, spaces&nbsp;<code>' '<\/code>, and\/or dashes&nbsp;<code>'-'<\/code>.<\/p>\n\n\n\n<p>You would like to reformat the phone number in a certain manner. Firstly,&nbsp;<strong>remove<\/strong>&nbsp;all spaces and dashes. Then,&nbsp;<strong>group<\/strong>&nbsp;the digits from left to right into blocks of length 3&nbsp;<strong>until<\/strong>&nbsp;there are 4 or fewer digits. The final digits are then grouped as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>2 digits: A single block of length 2.<\/li><li>3 digits: A single block of length 3.<\/li><li>4 digits: Two blocks of length 2 each.<\/li><\/ul>\n\n\n\n<p>The blocks are then joined by dashes. Notice that the reformatting process should&nbsp;<strong>never<\/strong>&nbsp;produce any blocks of length 1 and produce&nbsp;<strong>at most<\/strong>&nbsp;two blocks of length 2.<\/p>\n\n\n\n<p>Return&nbsp;<em>the phone number after formatting.<\/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> number = \"1-23-45 6\"\n<strong>Output:<\/strong> \"123-456\"\n<strong>Explanation:<\/strong> The digits are \"123456\".\nStep 1: There are more than 4 digits, so group the next 3 digits. The 1st block is \"123\".\nStep 2: There are 3 digits remaining, so put them in a single block of length 3. The 2nd block is \"456\".\nJoining the blocks gives \"123-456\".\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> number = \"123 4-567\"\n<strong>Output:<\/strong> \"123-45-67\"\n<strong>Explanation: <\/strong>The digits are \"1234567\".\nStep 1: There are more than 4 digits, so group the next 3 digits. The 1st block is \"123\".\nStep 2: There are 4 digits left, so split them into two blocks of length 2. The blocks are \"45\" and \"67\".\nJoining the blocks gives \"123-45-67\".\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> number = \"123 4-5678\"\n<strong>Output:<\/strong> \"123-456-78\"\n<strong>Explanation:<\/strong> The digits are \"12345678\".\nStep 1: The 1st block is \"123\".\nStep 2: The 2nd block is \"456\".\nStep 3: There are 2 digits left, so put them in a single block of length 2. The 3rd block is \"78\".\nJoining the blocks gives \"123-456-78\".\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> number = \"12\"\n<strong>Output:<\/strong> \"12\"\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> number = \"--17-5 229 35-39475 \"\n<strong>Output:<\/strong> \"175-229-353-94-75\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= number.length &lt;= 100<\/code><\/li><li><code>number<\/code>&nbsp;consists of digits&nbsp;and the characters&nbsp;<code>'-'<\/code>&nbsp;and&nbsp;<code>' '<\/code>.<\/li><li>There are at least&nbsp;<strong>two<\/strong>&nbsp;digits in&nbsp;<code>number<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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 reformatNumber(string number) {\n    string ans;\n    const int total = count_if(begin(number), end(number), \n                               [](char c) { return isdigit(c); });\n    int l = 0;\n    for (char c : number) {\n      if (!isdigit(c)) continue;      \n      ans += c;\n      ++l;\n      if ((l % 3 == 0 && total - l >= 4) ||\n          (total % 3 != 0 && total - l == 2)\n          || (total % 3 == 0 && total - l == 3))\n        ans += \"-\";\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a phone number as a string&nbsp;number.&nbsp;number&nbsp;consists of digits, spaces&nbsp;&#8216; &#8216;, and\/or dashes&nbsp;&#8216;-&#8216;. You would like to reformat the phone number in a&#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":[222,297,4],"class_list":["post-7820","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-format","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7820","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=7820"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7820\/revisions"}],"predecessor-version":[{"id":7822,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7820\/revisions\/7822"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}