<?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>split Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/split/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/split/</link>
	<description></description>
	<lastBuildDate>Sun, 09 Jan 2022 07:30:08 +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>split Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/split/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2129. Capitalize the Title</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2129-capitalize-the-title/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2129-capitalize-the-title/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 09 Jan 2022 07:26:24 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[words]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9411</guid>

					<description><![CDATA[<p>You are given a string&#160;title&#160;consisting of one or more words separated by a single space, where each word consists of English letters.&#160;Capitalize&#160;the string by changing&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2129-capitalize-the-title/">花花酱 LeetCode 2129. Capitalize the Title</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a string&nbsp;<code>title</code>&nbsp;consisting of one or more words separated by a single space, where each word consists of English letters.&nbsp;<strong>Capitalize</strong>&nbsp;the string by changing the capitalization of each word such that:</p>



<ul><li>If the length of the word is&nbsp;<code>1</code>&nbsp;or&nbsp;<code>2</code>&nbsp;letters, change all letters to lowercase.</li><li>Otherwise, change the first letter to uppercase and the remaining letters to lowercase.</li></ul>



<p>Return&nbsp;<em>the&nbsp;<strong>capitalized</strong>&nbsp;</em><code>title</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> title = "capiTalIze tHe titLe"
<strong>Output:</strong> "Capitalize The Title"
<strong>Explanation:</strong>
Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> title = "First leTTeR of EACH Word"
<strong>Output:</strong> "First Letter of Each Word"
<strong>Explanation:</strong>
The word "of" has length 2, so it is all lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> title = "i lOve leetcode"
<strong>Output:</strong> "i Love Leetcode"
<strong>Explanation:</strong>
The word "i" has length 1, so it is lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= title.length &lt;= 100</code></li><li><code>title</code>&nbsp;consists of words separated by a single space without any leading or trailing spaces.</li><li>Each word consists of uppercase and lowercase English letters and is&nbsp;<strong>non-empty</strong>.</li></ul>



<h2><strong>Solution: Straight forward</strong></h2>



<p>Without splitting the sentence into words, we need to take care the word of length one and two.</p>



<p>Tips: use std::tolower, std::toupper to transform letters.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string capitalizeTitle(string title) {
    const int n = title.size();
    for (int i = 0; i &lt; title.size(); ++i) {
      if ((i == 0 || title[i - 1] == ' ') 
          &amp;&amp; i + 2 &lt; n &amp;&amp; title[i + 1] != ' ' &amp;&amp; title[i + 2] != ' ')
        title[i] = toupper(title[i]);
      else
        title[i] = tolower(title[i]);         
    }
    return title;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def capitalizeTitle(self, title: str) -&gt; str:
    return &quot; &quot;.join(map(lambda w: w.lower() if len(w) &lt;= 2 else w[0].upper() + w[1:].lower(), title.split()))</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2129-capitalize-the-title/">花花酱 LeetCode 2129. Capitalize the Title</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-2129-capitalize-the-title/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2042. Check if Numbers Are Ascending in a Sentence</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2042-check-if-numbers-are-ascending-in-a-sentence/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2042-check-if-numbers-are-ascending-in-a-sentence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 18 Oct 2021 03:33:51 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8604</guid>

					<description><![CDATA[<p>A sentence is a list of&#160;tokens&#160;separated by a&#160;single&#160;space with no leading or trailing spaces. Every token is either a&#160;positive number&#160;consisting of digits&#160;0-9&#160;with no leading zeros,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2042-check-if-numbers-are-ascending-in-a-sentence/">花花酱 LeetCode 2042. Check if Numbers Are Ascending in a Sentence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A sentence is a list of&nbsp;<strong>tokens</strong>&nbsp;separated by a&nbsp;<strong>single</strong>&nbsp;space with no leading or trailing spaces. Every token is either a&nbsp;<strong>positive number</strong>&nbsp;consisting of digits&nbsp;<code>0-9</code>&nbsp;with no leading zeros, or a&nbsp;<strong>word</strong>&nbsp;consisting of lowercase English letters.</p>



<ul><li>For example,&nbsp;<code>"a puppy has 2 eyes 4 legs"</code>&nbsp;is a sentence with seven tokens:&nbsp;<code>"2"</code>&nbsp;and&nbsp;<code>"4"</code>&nbsp;are numbers and the other tokens such as&nbsp;<code>"puppy"</code>&nbsp;are words.</li></ul>



<p>Given a string&nbsp;<code>s</code>&nbsp;representing a sentence, you need to check if&nbsp;<strong>all</strong>&nbsp;the numbers in&nbsp;<code>s</code>&nbsp;are&nbsp;<strong>strictly increasing</strong>&nbsp;from left to right (i.e., other than the last number,&nbsp;<strong>each</strong>&nbsp;number is&nbsp;<strong>strictly smaller</strong>&nbsp;than the number on its&nbsp;<strong>right</strong>&nbsp;in&nbsp;<code>s</code>).</p>



<p>Return&nbsp;<code>true</code><em>&nbsp;if so, or&nbsp;</em><code>false</code><em>&nbsp;otherwise</em>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/09/30/example1.png" alt="example-1"/></figure>



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles"
<strong>Output:</strong> true
<strong>Explanation:</strong> The numbers in s are: 1, 3, 4, 6, 12.
They are strictly increasing from left to right: 1 &lt; 3 &lt; 4 &lt; 6 &lt; 12.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> s = "hello world 5 x 5"
<strong>Output:</strong> false
<strong>Explanation:</strong> The numbers in s are: <strong>5</strong>, <strong><u>5</u></strong>. They are not strictly increasing.
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/09/30/example3.png" alt="example-3"/></figure>



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s"
<strong>Output:</strong> false
<strong>Explanation:</strong> The numbers in s are: 7, <strong>51</strong>, <strong>50</strong>, 60. They are not strictly increasing.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> s = "4 5 11 26"
<strong>Output:</strong> true
<strong>Explanation:</strong> The numbers in s are: 4, 5, 11, 26.
They are strictly increasing from left to right: 4 &lt; 5 &lt; 11 &lt; 26.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>3 &lt;= s.length &lt;= 200</code></li><li><code>s</code>&nbsp;consists of lowercase English letters, spaces, and digits from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>9</code>, inclusive.</li><li>The number of tokens in&nbsp;<code>s</code>&nbsp;is between&nbsp;<code>2</code>&nbsp;and&nbsp;<code>100</code>, inclusive.</li><li>The tokens in&nbsp;<code>s</code>&nbsp;are separated by a single space.</li><li>There are at least&nbsp;<strong>two</strong>&nbsp;numbers in&nbsp;<code>s</code>.</li><li>Each number in&nbsp;<code>s</code>&nbsp;is a&nbsp;<strong>positive</strong>&nbsp;number&nbsp;<strong>less</strong>&nbsp;than&nbsp;<code>100</code>, with no leading zeros.</li><li><code>s</code>&nbsp;contains no leading or trailing spaces.</li></ul>



<p>Solution: String</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool areNumbersAscending(string s) {
    stringstream ss(s);
    string token;
    int last = -1;
    while (ss &gt;&gt; token) {
      if (isdigit(token[0])) {
        int num = stoi(token);
        if (num &lt;= last) return false;
        last = num;
      }
    }
    return true;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2042-check-if-numbers-are-ascending-in-a-sentence/">花花酱 LeetCode 2042. Check if Numbers Are Ascending in a Sentence</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-2042-check-if-numbers-are-ascending-in-a-sentence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1816. Truncate Sentence</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1816-truncate-sentence/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1816-truncate-sentence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Apr 2021 23:55:15 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8320</guid>

					<description><![CDATA[<p>A&#160;sentence&#160;is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of&#160;only&#160;uppercase and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1816-truncate-sentence/">花花酱 LeetCode 1816. Truncate Sentence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A&nbsp;<strong>sentence</strong>&nbsp;is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of&nbsp;<strong>only</strong>&nbsp;uppercase and lowercase English letters (no punctuation).</p>



<ul><li>For example,&nbsp;<code>"Hello World"</code>,&nbsp;<code>"HELLO"</code>, and&nbsp;<code>"hello world hello world"</code>&nbsp;are all sentences.</li></ul>



<p>You are given a sentence&nbsp;<code>s</code>​​​​​​ and an integer&nbsp;<code>k</code>​​​​​​. You want to&nbsp;<strong>truncate</strong>&nbsp;<code>s</code>​​​​​​ such that it contains only the&nbsp;<strong>first</strong>&nbsp;<code>k</code>​​​​​​ words. Return&nbsp;<code>s</code>​​​​<em>​​ after&nbsp;<strong>truncating</strong>&nbsp;it.</em></p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "Hello how are you Contestant", k = 4
<strong>Output:</strong> "Hello how are you"
<strong>Explanation:</strong>
The words in s are ["Hello", "how" "are", "you", "Contestant"].
The first 4 words are ["Hello", "how", "are", "you"].
Hence, you should return "Hello how are you".
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "What is the solution to this problem", k = 4
<strong>Output:</strong> "What is the solution"
<strong>Explanation:</strong>
The words in s are ["What", "is" "the", "solution", "to", "this", "problem"].
The first 4 words are ["What", "is", "the", "solution"].
Hence, you should return "What is the solution".</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "chopper is not a tanuki", k = 5
<strong>Output:</strong> "chopper is not a tanuki"
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= s.length &lt;= 500</code></li><li><code>k</code>&nbsp;is in the range&nbsp;<code>[1, the number of words in s]</code>.</li><li><code>s</code>&nbsp;consist of only lowercase and uppercase English letters and spaces.</li><li>The words in&nbsp;<code>s</code>&nbsp;are separated by a single space.</li><li>There are no leading or trailing spaces.</li></ul>



<h2><strong>Solution: </strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  string truncateSentence(string s, int k) {
    string ans;
    stringstream ss(s);
    for (int i = 0; i &lt; k &amp;&amp; ss; ++i) {
      string word;
      ss &gt;&gt; word;
      ans += (ans.empty() ? &quot;&quot; : &quot; &quot;) + word;
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def truncateSentence(self, s: str, k: int) -&gt; str:
    return &quot; &quot;.join(s.split()[:k])</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1816-truncate-sentence/">花花酱 LeetCode 1816. Truncate Sentence</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-1816-truncate-sentence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1556. Thousand Separator</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1556-thousand-separator/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1556-thousand-separator/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 22 Aug 2020 18:18:35 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7272</guid>

					<description><![CDATA[<p>Given an&#160;integer&#160;n, add a dot (&#8220;.&#8221;)&#160;as the thousands separator and return it in&#160;string format. Example 1: Input: n = 987 Output: "987" Example 2: Input:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1556-thousand-separator/">花花酱 LeetCode 1556. Thousand Separator</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an&nbsp;integer&nbsp;<code>n</code>, add a dot (&#8220;.&#8221;)&nbsp;as the thousands separator and return it in&nbsp;string format.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 987
<strong>Output:</strong> "987"
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 1234
<strong>Output:</strong> "1.234"
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 123456789
<strong>Output:</strong> "123.456.789"
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 0
<strong>Output:</strong> "0"
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>0 &lt;= n &lt; 2^31</code></li></ul>



<p><strong>Solution: Digit by digit</strong></p>



<p>Time complexity: O(log^2(n)) -> O(logn)<br>Space complexity: O(log(n))</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
class Solution {
public:
  string thousandSeparator(int n) {    
    string ans;
    int count = 0;
    do {
      if (count++ % 3 == 0 &amp;&amp; ans.size())
        ans = &quot;.&quot; + ans;
      ans = to_string(n % 10) + ans;
      n /= 10;      
    } while (n);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1556-thousand-separator/">花花酱 LeetCode 1556. Thousand Separator</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-1556-thousand-separator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1507. Reformat Date</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1507-reformat-date/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1507-reformat-date/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Jul 2020 03:58:04 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7067</guid>

					<description><![CDATA[<p>Given a&#160;date&#160;string in the form&#160;Day Month Year, where: Day&#160;is in the set&#160;{"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}. Month&#160;is in the set&#160;{"Jan", "Feb", "Mar", "Apr",&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1507-reformat-date/">花花酱 LeetCode 1507. Reformat Date</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<code>date</code>&nbsp;string in the form&nbsp;<code>Day Month Year</code>, where:</p>



<ul><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>



<p>Convert the date string to the format&nbsp;<code>YYYY-MM-DD</code>, where:</p>



<ul><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>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> date = "20th Oct 2052"
<strong>Output:</strong> "2052-10-20"
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> date = "6th Jun 1933"
<strong>Output:</strong> "1933-06-06"
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> date = "26th May 1960"
<strong>Output:</strong> "1960-05-26"
</pre>



<p><strong>Constraints:</strong></p>



<ul><li>The given dates are guaranteed to be valid, so no error handling is necessary.</li></ul>



<h2><strong>Solution: String + HashTable</strong></h2>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string reformatDate(string date) {
    stringstream ss(date);
    string day, month, year;
    ss &gt;&gt; day &gt;&gt; month &gt;&gt; year;
    unordered_map&lt;string, string&gt; m{{&quot;Jan&quot;, &quot;01&quot;},
                                    {&quot;Feb&quot;, &quot;02&quot;},
                                    {&quot;Mar&quot;, &quot;03&quot;},
                                    {&quot;Apr&quot;, &quot;04&quot;},
                                    {&quot;May&quot;, &quot;05&quot;},
                                    {&quot;Jun&quot;, &quot;06&quot;},
                                    {&quot;Jul&quot;, &quot;07&quot;},
                                    {&quot;Aug&quot;, &quot;08&quot;},
                                    {&quot;Sep&quot;, &quot;09&quot;},
                                    {&quot;Oct&quot;, &quot;10&quot;},
                                    {&quot;Nov&quot;, &quot;11&quot;},
                                    {&quot;Dec&quot;, &quot;12&quot;}};
    day = day.substr(0, day.length() - 2);
    if (day.length() == 1) day = &quot;0&quot; + day;
    return year + &quot;-&quot; + m[month] + &quot;-&quot; + day;
  }
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  public String reformatDate(String date) {
    Map&lt;String, String&gt; m = new HashMap&lt;String, String&gt;();
    m.put(&quot;Jan&quot;, &quot;01&quot;);
    m.put(&quot;Feb&quot;, &quot;02&quot;);
    m.put(&quot;Mar&quot;, &quot;03&quot;);
    m.put(&quot;Apr&quot;, &quot;04&quot;);
    m.put(&quot;May&quot;, &quot;05&quot;);
    m.put(&quot;Jun&quot;, &quot;06&quot;);
    m.put(&quot;Jul&quot;, &quot;07&quot;);
    m.put(&quot;Aug&quot;, &quot;08&quot;);
    m.put(&quot;Sep&quot;, &quot;09&quot;);
    m.put(&quot;Oct&quot;, &quot;10&quot;);
    m.put(&quot;Nov&quot;, &quot;11&quot;);
    m.put(&quot;Dec&quot;, &quot;12&quot;);
    String[] items = date.split(&quot; &quot;);    
    String day = items[0].substring(0, items[0].length() - 2);
    if (day.length() == 1) day = &quot;0&quot; + day;
    return items[2] + &quot;-&quot; + m.get(items[1]) + &quot;-&quot; + day;
  }
}</pre>

</div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def reformatDate(self, date: str) -&gt; str:
    m = {&quot;Jan&quot;: &quot;01&quot;, &quot;Feb&quot;: &quot;02&quot;, &quot;Mar&quot;: &quot;03&quot;, 
         &quot;Apr&quot;: &quot;04&quot;, &quot;May&quot;: &quot;05&quot;, &quot;Jun&quot;: &quot;06&quot;, 
         &quot;Jul&quot;: &quot;07&quot;, &quot;Aug&quot;: &quot;08&quot;, &quot;Sep&quot;: &quot;09&quot;, 
         &quot;Oct&quot;: &quot;10&quot;, &quot;Nov&quot;: &quot;11&quot;, &quot;Dec&quot;: &quot;12&quot;}
    items = date.split(&quot; &quot;)
    day = items[0][:-2]
    if len(day) == 1: day = &quot;0&quot; + day
    return items[2] + &quot;-&quot; + m[items[1]] + &quot;-&quot; + day</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1507-reformat-date/">花花酱 LeetCode 1507. Reformat Date</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-1507-reformat-date/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1416. Restore The Array</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1416-restore-the-array/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1416-restore-the-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 18 Apr 2020 20:20:41 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6638</guid>

					<description><![CDATA[<p>A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1416-restore-the-array/">花花酱 LeetCode 1416. Restore The Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1416. Restore The Array - 刷题找工作 EP320" width="500" height="375" src="https://www.youtube.com/embed/mdUTRI2FMtU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range&nbsp;<code>[1, k]</code>&nbsp;and there are no leading zeros in the array.</p>



<p>Given the string&nbsp;<code>s</code>&nbsp;and the integer&nbsp;<code>k</code>. There can be multiple ways to restore the array.</p>



<p>Return&nbsp;<em>the number of possible array</em>&nbsp;that can be printed as a string&nbsp;<code>s</code>&nbsp;using the mentioned program.</p>



<p>The number of ways could be very large so return it&nbsp;<strong>modulo</strong>&nbsp;<code>10^9 + 7</code></p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1000", k = 10000
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only possible array is [1000]
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1000", k = 10
<strong>Output:</strong> 0
<strong>Explanation:</strong> There cannot be an array that was printed this way and has all integer &gt;= 1 and &lt;= 10.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1317", k = 2000
<strong>Output:</strong> 8
<strong>Explanation:</strong> Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "2020", k = 30
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only possible array is [20,20]. [2020] is invalid because 2020 &gt; 30. [2,020] is ivalid because 020 contains leading zeros.
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1234567890", k = 90
<strong>Output:</strong> 34
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= s.length &lt;= 10^5</code>.</li><li><code>s</code>&nbsp;consists of only digits and doesn&#8217;t contain leading zeros.</li><li><code>1 &lt;= k &lt;= 10^9</code>.</li></ul>



<h2><strong>Solution: DP</strong></h2>



<p>dp[i] := # of ways to restore the array for s[i:n].</p>



<p>dp[i] = sum(dp[j]), where 0 &lt; j &lt; n, int(s[i:j]) &lt;= k, s[i] != 0</p>



<p>Time complexity: O(n*logk)<br>Space complexity: O(n)</p>



<p>Top-down</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numberOfArrays(string s, int k) {
    constexpr int kMod = 1e9 + 7;
    const int n = s.length();
    vector&lt;int&gt; mem(n, -1);
    // dp(i) returns # of ways for s[i:n]
    function&lt;int(int)&gt; dp = [&amp;](int i) {
      if (i == n) return 1;
      if (s[i] == '0') return 0;
      if (mem[i] &gt;= 0) return mem[i];
      long num = 0;
      int ans = 0;
      for (int j = i + 1; j &lt;= n; ++j) {
        num = num * 10 + (s[j - 1] - '0');
        if (num &gt; k) break;
        ans = (ans + dp(j)) % kMod;
      }
      return mem[i] = ans;
    };
    return dp(0);
  }
};</pre>
</div></div>



<p>bottom-up</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numberOfArrays(string s, int k) {
    constexpr int kMod = 1e9 + 7;
    const int n = s.length();
    vector&lt;int&gt; dp(n + 1); // # of ways for s[i:n]
    dp.back() = 1;
    for (int i = n - 1; i &gt;= 0; --i) {
      if (s[i] == '0') continue;
      long num = 0;
      for (int j = i + 1; j &lt;= n; ++j) {
        num = num * 10 + (s[j - 1] - '0');
        if (num &gt; k) break;
        dp[i] = (dp[i] + dp[j]) % kMod;
      }
    }
    return dp[0];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1416-restore-the-array/">花花酱 LeetCode 1416. Restore The Array</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/dynamic-programming/leetcode-1416-restore-the-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 927. Three Equal Parts</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-927-three-equal-parts/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-927-three-equal-parts/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 21 Oct 2018 05:46:41 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[split]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4211</guid>

					<description><![CDATA[<p>Problem Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value. If it&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-927-three-equal-parts/">花花酱 LeetCode 927. Three Equal Parts</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>Given an array <code>A</code> of <code>0</code>s and <code>1</code>s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value.</p>
<p>If it is possible, return <strong>any</strong> <code>[i, j]</code> with <code>i+1 &lt; j</code>, such that:</p>
<ul>
<li><code>A[0], A[1], ..., A[i]</code> is the first part;</li>
<li><code>A[i+1], A[i+2], ..., A[j-1]</code> is the second part, and</li>
<li><code>A[j], A[j+1], ..., A[A.length - 1]</code> is the third part.</li>
<li>All three parts have equal binary value.</li>
</ul>
<p>If it is not possible, return <code>[-1, -1]</code>.</p>
<p>Note that the entire part is used when considering what binary value it represents.  For example, <code>[1,1,0]</code> represents <code>6</code> in decimal, not <code>3</code>.  Also, leading zeros are allowed, so <code>[0,1,1]</code> and <code>[1,1]</code> represent the same value.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[1,0,1,0,1]</span>
<strong>Output: </strong><span id="example-output-1">[0,3]</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[1,1,0,1,1]</span>
<strong>Output: </strong><span id="example-output-2">[-1,-1]</span></pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>3 &lt;= A.length &lt;= 30000</code></li>
<li><code>A[i] == 0</code> or <code>A[i] == 1</code></li>
</ol>
<h1><strong>Solution:</strong></h1>
<p>each part should have the same number of 1 s.</p>
<p>Find the suffix (without leading os) of the last part which should have 1/3 of the total ones.</p>
<p>Time complexity: O(n^2) in theory but close to O(n) in practice</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 44 ms
class Solution {
public:
  vector&lt;int&gt; threeEqualParts(vector&lt;int&gt;&amp; A) {    
    string s(begin(A), end(A));
    int ones = accumulate(begin(A), end(A), 0);
    if (ones % 3 != 0) return {-1, -1};
    if (ones == 0) return {0, A.size() - 1};
    ones /= 3;
    int right = A.size() - 1;
    while (ones) if (A[right--]) --ones;
    string suffix(begin(s) + right + 1, end(s));
    size_t l = suffix.length();
    size_t left = s.find(suffix);
    if (left == std::string::npos) return {-1, -1};
    size_t mid = s.find(suffix, left + l);
    if (mid == std::string::npos 
       || mid + 2 * l &gt; s.length()) return {-1, -1};
    return {left + l - 1, mid + l};
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-927-three-equal-parts/">花花酱 LeetCode 927. Three Equal Parts</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-927-three-equal-parts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 805. Split Array With Same Average</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-805-split-array-with-same-average/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-805-split-array-with-same-average/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 25 Mar 2018 02:46:20 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2365</guid>

					<description><![CDATA[<p>Problem 题目大意：问能否将一个数组分成两部分，每部分的平均值相同。 In a given integer array A, we must move every element of A to either list B or list C. (B and C&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-805-split-array-with-same-average/">花花酱 LeetCode 805. Split Array With Same Average</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>题目大意：问能否将一个数组分成两部分，每部分的平均值相同。</p>
<p>In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.)</p>
<p>Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty.</p>
<pre class="crayon:false "><strong>Example :</strong>
<strong>Input:</strong> 
[1,2,3,4,5,6,7,8]
<strong>Output:</strong> true
<strong>Explanation: </strong>We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have the average of 4.5.
</pre>
<p><strong>Note:</strong></p>
<ul>
<li>The length of <code>A</code> will be in the range [1, 30].</li>
<li><code>A[i]</code> will be in the range of <code>[0, 10000]</code>.</li>
</ul>
<h1><strong>Solution: Search</strong></h1>
<p>Time complexity: O(2^n)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 771 ms
class Solution {
public:
  bool splitArraySameAverage(vector&lt;int&gt;&amp; A) {
    std::sort(A.begin(), A.end());
    sum = std::accumulate(A.begin(), A.end(), 0);
    n = A.size();
    return dfs(A, 1, 0, 0);
  }
private:
  int sum;
  int n;
  bool dfs(const vector&lt;int&gt;&amp; A, int c, int s, int cur) {
    if (c &gt; A.size() / 2) return false;
    for (int i = s; i &lt; A.size(); ++i) {
      cur += A[i];      
      if (cur * (n - c)  == (sum - cur) * c) return true;
      if (cur * (n - c)  &gt; (sum - cur) * c) break;
      if (dfs(A, c + 1, i + 1, cur)) return true;
      cur -= A[i];
    }
    return false;
  } 
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-805-split-array-with-same-average/">花花酱 LeetCode 805. Split Array With Same Average</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/searching/leetcode-805-split-array-with-same-average/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
