<?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>tire Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/tire/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/tire/</link>
	<description></description>
	<lastBuildDate>Sun, 18 Sep 2022 20:33:53 +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>tire Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/tire/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2416. Sum of Prefix Scores of Strings</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-2416-sum-of-prefix-scores-of-strings/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-2416-sum-of-prefix-scores-of-strings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 18 Sep 2022 19:44:55 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[prefix]]></category>
		<category><![CDATA[tire]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9832</guid>

					<description><![CDATA[<p>You are given an array&#160;words&#160;of size&#160;n&#160;consisting of&#160;non-empty&#160;strings. We define the&#160;score&#160;of a string&#160;word&#160;as the&#160;number&#160;of strings&#160;words[i]&#160;such that&#160;word&#160;is a&#160;prefix&#160;of&#160;words[i]. For example, if&#160;words = ["a", "ab", "abc", "cab"], then&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-2416-sum-of-prefix-scores-of-strings/">花花酱 LeetCode 2416. Sum of Prefix Scores of Strings</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 is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 2416. Sum of Prefix Scores of Strings - 刷题找工作 EP402" width="500" height="281" src="https://www.youtube.com/embed/OJ0PMH6M2MQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given an array&nbsp;<code>words</code>&nbsp;of size&nbsp;<code>n</code>&nbsp;consisting of&nbsp;<strong>non-empty</strong>&nbsp;strings.</p>



<p>We define the&nbsp;<strong>score</strong>&nbsp;of a string&nbsp;<code>word</code>&nbsp;as the&nbsp;<strong>number</strong>&nbsp;of strings&nbsp;<code>words[i]</code>&nbsp;such that&nbsp;<code>word</code>&nbsp;is a&nbsp;<strong>prefix</strong>&nbsp;of&nbsp;<code>words[i]</code>.</p>



<ul><li>For example, if&nbsp;<code>words = ["a", "ab", "abc", "cab"]</code>, then the score of&nbsp;<code>"ab"</code>&nbsp;is&nbsp;<code>2</code>, since&nbsp;<code>"ab"</code>&nbsp;is a prefix of both&nbsp;<code>"ab"</code>&nbsp;and&nbsp;<code>"abc"</code>.</li></ul>



<p>Return&nbsp;<em>an array&nbsp;</em><code>answer</code><em>&nbsp;of size&nbsp;</em><code>n</code><em>&nbsp;where&nbsp;</em><code>answer[i]</code><em>&nbsp;is the&nbsp;<strong>sum</strong>&nbsp;of scores of every&nbsp;<strong>non-empty</strong>&nbsp;prefix of&nbsp;</em><code>words[i]</code>.</p>



<p><strong>Note</strong>&nbsp;that a string is considered as a prefix of itself.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["abc","ab","bc","b"]
<strong>Output:</strong> [5,4,3,2]
<strong>Explanation:</strong> The answer for each string is the following:
- "abc" has 3 prefixes: "a", "ab", and "abc".
- There are 2 strings with the prefix "a", 2 strings with the prefix "ab", and 1 string with the prefix "abc".
The total is answer[0] = 2 + 2 + 1 = 5.
- "ab" has 2 prefixes: "a" and "ab".
- There are 2 strings with the prefix "a", and 2 strings with the prefix "ab".
The total is answer[1] = 2 + 2 = 4.
- "bc" has 2 prefixes: "b" and "bc".
- There are 2 strings with the prefix "b", and 1 string with the prefix "bc".
The total is answer[2] = 2 + 1 = 3.
- "b" has 1 prefix: "b".
- There are 2 strings with the prefix "b".
The total is answer[3] = 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["abcd"]
<strong>Output:</strong> [4]
<strong>Explanation:</strong>
"abcd" has 4 prefixes: "a", "ab", "abc", and "abcd".
Each prefix has a score of one, so the total is answer[0] = 1 + 1 + 1 + 1 = 4.
</pre>



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



<ul><li><code>1 &lt;= words.length &lt;= 1000</code></li><li><code>1 &lt;= words[i].length &lt;= 1000</code></li><li><code>words[i]</code>&nbsp;consists of lowercase English letters.</li></ul>



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



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s1.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s1.png" alt="" class="wp-image-9836" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s2.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s2.png" alt="" class="wp-image-9837" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s3.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s3.png" alt="" class="wp-image-9839" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/09/lc-2416-ep402-s3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>Insert all the words into a tire whose node val is the number of substrings that have the current prefix.</p>



<p>During query time, sum up the values along the prefix path.</p>



<p>Time complexity: O(sum(len(word))<br>Space complexity: O(sum(len(word))</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
struct Trie {
  Trie* ch[26] = {};
  int cnt = 0;
  void insert(string_view s) {
    Trie* cur = this;
    for (char c : s) {
      if (!cur-&gt;ch[c - 'a']) 
        cur-&gt;ch[c - 'a'] = new Trie();
      cur = cur-&gt;ch[c - 'a'];
      ++cur-&gt;cnt;
    }
  }
  int query(string_view s) {
    Trie* cur = this;
    int ans = 0;
    for (char c : s) {
      cur = cur-&gt;ch[c - 'a'];
      ans += cur-&gt;cnt;
    }
    return ans;
  }
};
class Solution {
public:
  vector&lt;int&gt; sumPrefixScores(vector&lt;string&gt;&amp; words) {
    Trie* root = new Trie();
    for (const string&amp; w : words)
      root-&gt;insert(w);
    vector&lt;int&gt; ans;
    for (const string&amp; w : words)
      ans.push_back(root-&gt;query(w));
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-2416-sum-of-prefix-scores-of-strings/">花花酱 LeetCode 2416. Sum of Prefix Scores of Strings</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-2416-sum-of-prefix-scores-of-strings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
