<?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>string_view &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/string_view/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog</link>
	<description></description>
	<lastBuildDate>Fri, 11 Apr 2025 16:25:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>string_view &#8211; Huahua&#8217;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2023. Number of Pairs of Strings With Concatenation Equal to Target</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2023-number-of-pairs-of-strings-with-concatenation-equal-to-target/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2023-number-of-pairs-of-strings-with-concatenation-equal-to-target/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 11 Apr 2025 16:24:45 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[string_view]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10337</guid>

					<description><![CDATA[方法1: Brute Force 枚举所有的(nums[i], nums[j])组合，相加在和target比较。 时间复杂度：O(mn2) m为字符串的最长长度。空间复杂度：O(m) 优化前 67ms, 49.3M [crayon-67f993f679bd6236306114/] 一些工程上的优化 优化后 3ms, 12.88MB [crayon-67f993f679be7170421174/]]]></description>
										<content:encoded><![CDATA[
<p>方法1: Brute Force</p>



<p>枚举所有的(nums[i], nums[j])组合，相加在和target比较。</p>



<p>时间复杂度：O(mn<sup>2</sup>) m为字符串的最长长度。<br>空间复杂度：O(m)</p>



<p>优化前 67ms, 49.3M</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int numOfPairs(vector&lt;string&gt;&amp; nums, string target) {
    const int n = nums.size();
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (i != j &amp;&amp; nums[i] + nums[j] == target)
          ++ans;
    return ans;
  }
};</pre>



<p>一些工程上的优化</p>



<ul class="wp-block-list">
<li>用string_view作为参数类型，减少一次string copy</li>



<li>先比较长度，再判断内容。</li>



<li>string_view.substr 是O(1)时间，和直接用strncmp比较内容是一样的。</li>
</ul>



<p>优化后 3ms, 12.88MB</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int numOfPairs(vector&lt;string&gt;&amp; nums, string_view target) {
    const int n = nums.size();
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (i != j 
            &amp;&amp; nums[i].size() + nums[j].size() == target.size()
            &amp;&amp; target.substr(0, nums[i].size()) == nums[i]
            &amp;&amp; target.substr(nums[i].size()) == nums[j])
              ++ans;
    return ans;
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-2023-number-of-pairs-of-strings-with-concatenation-equal-to-target/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
