{"id":2508,"date":"2018-04-16T08:53:24","date_gmt":"2018-04-16T15:53:24","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2508"},"modified":"2018-04-17T23:35:07","modified_gmt":"2018-04-18T06:35:07","slug":"leetcode-482-license-key-formatting","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-482-license-key-formatting\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 482. License Key Formatting"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u628a\u5e8f\u5217\u53f7\u683c\u5f0f\u5316\uff0c\u6bcf\u7ec4\u8981\u6c42\u662fK\u4e2a\u5b57\u7b26\uff08\u7b2c\u4e00\u7ec4 &lt;= K\uff09\uff0c\u7ec4\u548c\u7ec4\u4e4b\u95f4\u7528\u201c-\u201d\u5206\u9694\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/license-key-formatting\/description\/\">https:\/\/leetcode.com\/problems\/license-key-formatting\/description\/<\/a><\/p>\n<p>You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.<\/p>\n<p>Given a number K, we would want to reformat the strings such that each group contains\u00a0<i>exactly<\/i>\u00a0K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.<\/p>\n<p>Given a non-empty string S and a number K, format the string according to the rules described above.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> S = \"5F3Z-2e-9-w\", K = 4\r\n\r\n<b>Output:<\/b> \"5F3Z-2E9W\"\r\n\r\n<b>Explanation:<\/b> The string S has been split into two parts, each part has 4 characters.\r\nNote that the two extra dashes are not needed and can be removed.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> S = \"2-5g-3-J\", K = 2\r\n\r\n<b>Output:<\/b> \"2-5G-3J\"\r\n\r\n<b>Explanation:<\/b> The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of string S will not exceed 12,000, and K is a positive integer.<\/li>\n<li>String S consists only of alphanumerical characters (a-z and\/or A-Z and\/or 0-9) and dashes(-).<\/li>\n<li>String S is non-empty.<\/li>\n<\/ol>\n<p><ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-2404451723245401\"\n     data-ad-slot=\"7983117522\"><\/ins><\/p>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n    string licenseKeyFormatting(string S, int K) {\r\n      string s;\r\n      for (char c : S)\r\n        if (c != '-') s.push_back(toupper(c));\r\n      int first = s.length() % K;\r\n      string ans = s.substr(0, first);\r\n      for (int i = 0; i &lt; s.length() - first; ++i) {\r\n        if (i % K == 0 &amp;&amp; (i + first)) ans += '-';\r\n        ans.push_back(s[i + first]);\r\n      }\r\n      return ans;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u628a\u5e8f\u5217\u53f7\u683c\u5f0f\u5316\uff0c\u6bcf\u7ec4\u8981\u6c42\u662fK\u4e2a\u5b57\u7b26\uff08\u7b2c\u4e00\u7ec4 &lt;= K\uff09\uff0c\u7ec4\u548c\u7ec4\u4e4b\u95f4\u7528\u201c-\u201d\u5206\u9694\u3002 https:\/\/leetcode.com\/problems\/license-key-formatting\/description\/ You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string&#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,298,4],"class_list":["post-2508","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-formating","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2508","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=2508"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2508\/revisions"}],"predecessor-version":[{"id":2530,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2508\/revisions\/2530"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}