<?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>O(2^n) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/o2n/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/o2n/</link>
	<description></description>
	<lastBuildDate>Tue, 29 Oct 2019 07:04: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>O(2^n) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/o2n/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-1239-maximum-length-of-a-concatenated-string-with-unique-characters/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-1239-maximum-length-of-a-concatenated-string-with-unique-characters/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 28 Oct 2019 03:56:41 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(2^n)]]></category>
		<category><![CDATA[search]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5792</guid>

					<description><![CDATA[<p>Given an array of strings&#160;arr. String&#160;s&#160;is a concatenation of a sub-sequence of&#160;arr&#160;which have&#160;unique characters. Return&#160;the maximum possible length&#160;of&#160;s. Example 1: Input: arr = ["un","iq","ue"] Output:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1239-maximum-length-of-a-concatenated-string-with-unique-characters/">花花酱 LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters</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 array of strings&nbsp;<code>arr</code>. String&nbsp;<code>s</code>&nbsp;is a concatenation of a sub-sequence of&nbsp;<code>arr</code>&nbsp;which have&nbsp;<strong>unique characters</strong>.</p>



<p>Return&nbsp;<em>the maximum possible length</em>&nbsp;of&nbsp;<code>s</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = ["un","iq","ue"]
<strong>Output:</strong> 4
<strong>Explanation:</strong> All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = ["cha","r","act","ers"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> Possible solutions are "chaers" and "acters".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = ["abcdefghijklmnopqrstuvwxyz"]
<strong>Output:</strong> 26
</pre>



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



<ul><li><code>1 &lt;= arr.length &lt;= 16</code></li><li><code>1 &lt;= arr[i].length &lt;= 26</code></li><li><code>arr[i]</code>&nbsp;contains only lower case English letters.</li></ul>



<h2><strong>Solution: Combination + Bit</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxLength(vector&lt;string&gt;&amp; arr) {
    vector&lt;int&gt; a;
    
    for (const string&amp; x : arr) {
      set&lt;char&gt; s(begin(x), end(x));
      if (s.size() != x.length()) continue;
      a.push_back(0);      
      for (char c : x) a.back() |= 1 &lt;&lt; (c - 'a');      
    }
    
    int ans = 0;
    
    function&lt;void(int, int)&gt; dfs = [&amp;](int s, int cur) {
      ans = max(ans, __builtin_popcount(cur));
      for (int i = s; i &lt; a.size(); ++i)
        if ((cur &amp; a[i]) == 0)
          dfs(i + 1, cur | a[i]);      
    };
    
    dfs(0, 0);
    return ans;
  }
};</pre>
</div></div>



<p>Solution 2: DP</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxLength(vector&lt;string&gt;&amp; arr) {
    vector&lt;int&gt; a;
    
    for (const string&amp; x : arr) {
      int mask = 0;      
      for (char c : x) mask |= 1 &lt;&lt; (c - 'a');
      if (__builtin_popcount(mask) != x.length()) continue;
      a.push_back(mask);
    }
    
    int ans = 0;
    
    vector&lt;int&gt; dp{0};
    for (int i = 0; i &lt; a.size(); ++i) {
      int size = dp.size();
      for (int j = 0; j &lt; size; ++j) {
        if (dp[j] &amp; a[i]) continue;
        int t = dp[j] | a[i];        
        dp.push_back(t);
        ans = max(ans, __builtin_popcount(t));
      }
    }
    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1239-maximum-length-of-a-concatenated-string-with-unique-characters/">花花酱 LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters</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-1239-maximum-length-of-a-concatenated-string-with-unique-characters/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
