<?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>removal Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/removal/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/removal/</link>
	<description></description>
	<lastBuildDate>Sun, 10 Jan 2021 03:50:57 +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>removal Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/removal/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1717. Maximum Score From Removing Substrings</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1717-maximum-score-from-removing-substrings/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1717-maximum-score-from-removing-substrings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Jan 2021 03:12:39 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[removal]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7947</guid>

					<description><![CDATA[<p>You are given a string&#160;s&#160;and two integers&#160;x&#160;and&#160;y. You can perform two types of operations any number of times. Remove substring&#160;"ab"&#160;and gain&#160;x&#160;points. For example, when removing&#160;"ab"&#160;from&#160;"cabxbae"&#160;it&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1717-maximum-score-from-removing-substrings/">花花酱 LeetCode 1717. Maximum Score From Removing Substrings</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>s</code>&nbsp;and two integers&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>. You can perform two types of operations any number of times.</p>



<ul><li>Remove substring&nbsp;<code>"ab"</code>&nbsp;and gain&nbsp;<code>x</code>&nbsp;points.<ul><li>For example, when removing&nbsp;<code>"ab"</code>&nbsp;from&nbsp;<code>"c<u>ab</u>xbae"</code>&nbsp;it becomes&nbsp;<code>"cxbae"</code>.</li></ul></li><li>Remove substring&nbsp;<code>"ba"</code>&nbsp;and gain&nbsp;<code>y</code>&nbsp;points.<ul><li>For example, when removing&nbsp;<code>"ba"</code>&nbsp;from&nbsp;<code>"cabx<u>ba</u>e"</code>&nbsp;it becomes&nbsp;<code>"cabxe"</code>.</li></ul></li></ul>



<p>Return&nbsp;<em>the maximum points you can gain after applying the above operations on</em>&nbsp;<code>s</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>



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



<ul><li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= x, y &lt;= 10<sup>4</sup></code></li><li><code>s</code>&nbsp;consists of lowercase English letters.</li></ul>



<h2><strong>Solution: Greedy + Stack</strong></h2>



<p>Remove the pattern with the larger score first.</p>



<p>Using a stack to remove all occurrences of a pattern in place in O(n) Time.</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:
  int maximumGain(string s, int x, int y) {
    // Remove patttern p from s for t points each.
    // Returns the total score.
    auto remove = [&amp;](const string&amp; p, int t) {
      int total = 0;
      int i = 0;
      for (int j = 0; j &lt; s.size(); ++j, ++i) {
        s[i] = s[j];
        if (i &amp;&amp; s[i - 1] == p[0] &amp;&amp; s[i] == p[1]) {
          i -= 2;
          total += t;
        }
      }
      s.resize(i); 
      return total;
    };
    string px{&quot;ab&quot;};
    string py{&quot;ba&quot;};
    if (y &gt; x) {
      swap(px, py);
      swap(x, y);
    }    
    return remove(px, x) + remove(py, y);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1717-maximum-score-from-removing-substrings/">花花酱 LeetCode 1717. Maximum Score From Removing Substrings</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/greedy/leetcode-1717-maximum-score-from-removing-substrings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
