<?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>match Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/match/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/match/</link>
	<description></description>
	<lastBuildDate>Mon, 25 Oct 2021 06:55:01 +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>match Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/match/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2047. Number of Valid Words in a Sentence</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2047-number-of-valid-words-in-a-sentence/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2047-number-of-valid-words-in-a-sentence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 25 Oct 2021 06:47:43 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[match]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8637</guid>

					<description><![CDATA[<p>A sentence consists of lowercase letters ('a'&#160;to&#160;'z'), digits ('0'&#160;to&#160;'9'), hyphens ('-'), punctuation marks ('!',&#160;'.', and&#160;','), and spaces (' ') only. Each sentence can be broken&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2047-number-of-valid-words-in-a-sentence/">花花酱 LeetCode 2047. Number of Valid Words 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 consists of lowercase letters (<code>'a'</code>&nbsp;to&nbsp;<code>'z'</code>), digits (<code>'0'</code>&nbsp;to&nbsp;<code>'9'</code>), hyphens (<code>'-'</code>), punctuation marks (<code>'!'</code>,&nbsp;<code>'.'</code>, and&nbsp;<code>','</code>), and spaces (<code>' '</code>) only. Each sentence can be broken down into&nbsp;<strong>one or more tokens</strong>&nbsp;separated by one or more spaces&nbsp;<code>' '</code>.</p>



<p>A token is a valid word if:</p>



<ul><li>It only contains lowercase letters, hyphens, and/or punctuation (<strong>no</strong>&nbsp;digits).</li><li>There is&nbsp;<strong>at most one</strong>&nbsp;hyphen&nbsp;<code>'-'</code>. If present, it should be surrounded by lowercase characters (<code>"a-b"</code>&nbsp;is valid, but&nbsp;<code>"-ab"</code>&nbsp;and&nbsp;<code>"ab-"</code>&nbsp;are not valid).</li><li>There is&nbsp;<strong>at most one</strong>&nbsp;punctuation mark. If present, it should be at the&nbsp;<strong>end</strong>&nbsp;of the token.</li></ul>



<p>Examples of valid words include&nbsp;<code>"a-b."</code>,&nbsp;<code>"afad"</code>,&nbsp;<code>"ba-c"</code>,&nbsp;<code>"a!"</code>, and&nbsp;<code>"!"</code>.</p>



<p>Given a string&nbsp;<code>sentence</code>, return&nbsp;<em>the&nbsp;<strong>number</strong>&nbsp;of valid words in&nbsp;</em><code>sentence</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "cat and  dog"
<strong>Output:</strong> 3
<strong>Explanation:</strong> The valid words in the sentence are "cat", "and", and "dog".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "!this  1-s b8d!"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid words in the sentence.
"!this" is invalid because it starts with a punctuation mark.
"1-s" and "b8d" are invalid because they contain digits.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "alice and  bob are playing stone-game10"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The valid words in the sentence are "alice", "and", "bob", "are", and "playing".
"stone-game10" is invalid because it contains digits.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "he bought 2 pencils, 3 erasers, and 1  pencil-sharpener."
<strong>Output:</strong> 6
<strong>Explanation:</strong> The valid words in the sentence are "he", "bought", "pencils,", "erasers,", "and", and "pencil-sharpener.".
</pre>



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



<ul><li><code>1 &lt;= sentence.length &lt;= 1000</code></li><li><code>sentence</code>&nbsp;only contains lowercase English letters, digits,&nbsp;<code>' '</code>,&nbsp;<code>'-'</code>,&nbsp;<code>'!'</code>,&nbsp;<code>'.'</code>, and&nbsp;<code>','</code>.</li><li>There will be at least&nbsp;<code>1</code>&nbsp;token.</li></ul>



<h2><strong>Solution 1: Brute Force</strong></h2>



<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:
  int countValidWords(string sentence) {
    stringstream ss(sentence);
    string word;
    int ans = 0;
    while (ss &amp;gt;&amp;gt; word) {      
      bool valid = true;      
      int hyphen = 0;
      int punctuation = 0;
      char p = ' ';
      for (char c : word) {
        if (c == '-') {          
          if (++hyphen &amp;gt; 1 || !isalpha(p)) {
            valid = false;
            break;
          }
        } else if (c == '!' || c == '.' || c == ',') {
          if (++punctuation &amp;gt; 1 || p == '-') {
            valid = false;
            break;
          }
        } else if (isalpha(c)) {          
          if (punctuation) {
            valid = false;
            break;
          }
        } else {
          valid = false;
          break;
        }
        p = c;
      }
      if (word.back() == '-') 
        valid = false;
      if (valid) ++ans;      
    }
    return ans;
  }  
};</pre>
</div></div>



<h2><strong>Solution 2: Regex</strong></h2>



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



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def countValidWords(self, sentence: str) -&gt; int:
    ans = 0
    for word in sentence.split():
      if word.strip() and re.fullmatch('^([a-z]+(-?[a-z]+)?)?[\.,!]?$', word.strip()):
        ans += 1
    return ans</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2047-number-of-valid-words-in-a-sentence/">花花酱 LeetCode 2047. Number of Valid Words 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-2047-number-of-valid-words-in-a-sentence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1367. Linked List in Binary Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1367-linked-list-in-binary-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1367-linked-list-in-binary-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 02 Mar 2020 03:24:56 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[match]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6389</guid>

					<description><![CDATA[<p>Given a binary tree&#160;root&#160;and a&#160;linked list with&#160;head&#160;as the first node.&#160; Return True if all the elements in the linked list starting from the&#160;head&#160;correspond to some&#160;downward&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1367-linked-list-in-binary-tree/">花花酱 LeetCode 1367. Linked List in Binary Tree</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 binary tree&nbsp;<code>root</code>&nbsp;and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p>



<p>Return True if all the elements in the linked list starting from the&nbsp;<code>head</code>&nbsp;correspond to some&nbsp;<em>downward path</em>&nbsp;connected in the binary tree&nbsp;otherwise return False.</p>



<p>In this context downward path means a path that starts at some node and goes downwards.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree.  
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
<strong>Output:</strong> true
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>.
</pre>



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



<ul><li><code>1 &lt;= node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li><li>The given linked list will contain between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>100</code>&nbsp;nodes.</li><li>The given binary tree will contain between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>2500</code>&nbsp;nodes.</li></ul>



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



<p>We need two recursion functions: isSubPath / isPath, the later one does a strict match.</p>



<p>Time complexity: O(|L| * |T|)<br>Space complexity: O(|T|)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  bool isSubPath(ListNode* head, TreeNode* root) {
    if (!root) return false;
    return isPath(head, root) || isSubPath(head, root-&gt;left) || isSubPath(head, root-&gt;right);
  }
private:
  bool isPath(ListNode* head, TreeNode* root) {
    if (!head) return true;
    if (!root) return false;
    if (root-&gt;val != head-&gt;val) return false;
    return isPath(head-&gt;next, root-&gt;left) || isPath(head-&gt;next, root-&gt;right);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1367-linked-list-in-binary-tree/">花花酱 LeetCode 1367. Linked List in Binary Tree</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/tree/leetcode-1367-linked-list-in-binary-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
