<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>formating Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/formating/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/formating/</link>
	<description></description>
	<lastBuildDate>Wed, 18 Apr 2018 06:35:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>formating Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/formating/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 482. License Key Formatting</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-482-license-key-formatting/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-482-license-key-formatting/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 16 Apr 2018 15:53:24 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[formating]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2508</guid>

					<description><![CDATA[<p>Problem 题目大意：把序列号格式化，每组要求是K个字符（第一组 &#60;= K），组和组之间用“-”分隔。 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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-482-license-key-formatting/">花花酱 LeetCode 482. License Key Formatting</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>题目大意：把序列号格式化，每组要求是K个字符（第一组 &lt;= K），组和组之间用“-”分隔。</p>
<p><a href="https://leetcode.com/problems/license-key-formatting/description/">https://leetcode.com/problems/license-key-formatting/description/</a></p>
<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>
<p>Given a number K, we would want to reformat the strings such that each group contains <i>exactly</i> K 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>
<p>Given a non-empty string S and a number K, format the string according to the rules described above.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> S = "5F3Z-2e-9-w", K = 4

<b>Output:</b> "5F3Z-2E9W"

<b>Explanation:</b> The string S has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b> S = "2-5g-3-J", K = 2

<b>Output:</b> "2-5G-3J"

<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.
</pre>
<p><b>Note:</b></p>
<ol>
<li>The length of string S will not exceed 12,000, and K is a positive integer.</li>
<li>String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).</li>
<li>String S is non-empty.</li>
</ol>
<p><ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-2404451723245401"
     data-ad-slot="7983117522"></ins></p>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 16 ms
class Solution {
public:
    string licenseKeyFormatting(string S, int K) {
      string s;
      for (char c : S)
        if (c != '-') s.push_back(toupper(c));
      int first = s.length() % K;
      string ans = s.substr(0, first);
      for (int i = 0; i &lt; s.length() - first; ++i) {
        if (i % K == 0 &amp;&amp; (i + first)) ans += '-';
        ans.push_back(s[i + first]);
      }
      return ans;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-482-license-key-formatting/">花花酱 LeetCode 482. License Key Formatting</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-482-license-key-formatting/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
