<?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>replacing Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/replacing/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/replacing/</link>
	<description></description>
	<lastBuildDate>Fri, 31 Dec 2021 20:33:45 +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>replacing Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/replacing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1946. Largest Number After Mutating Substring</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1946-largest-number-after-mutating-substring/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1946-largest-number-after-mutating-substring/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 20:32:39 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[replacing]]></category>
		<category><![CDATA[substring]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9314</guid>

					<description><![CDATA[<p>You are given a string&#160;num, which represents a large integer. You are also given a&#160;0-indexed&#160;integer array&#160;change&#160;of length&#160;10&#160;that maps each digit&#160;0-9&#160;to another digit. More formally, digit&#160;d&#160;maps&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1946-largest-number-after-mutating-substring/">花花酱 LeetCode 1946. Largest Number After Mutating Substring</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>num</code>, which represents a large integer. You are also given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>change</code>&nbsp;of length&nbsp;<code>10</code>&nbsp;that maps each digit&nbsp;<code>0-9</code>&nbsp;to another digit. More formally, digit&nbsp;<code>d</code>&nbsp;maps to digit&nbsp;<code>change[d]</code>.</p>



<p>You may&nbsp;<strong>choose</strong>&nbsp;to&nbsp;<strong>mutate a single substring</strong>&nbsp;of&nbsp;<code>num</code>. To mutate a substring, replace each digit&nbsp;<code>num[i]</code>&nbsp;with the digit it maps to in&nbsp;<code>change</code>&nbsp;(i.e. replace&nbsp;<code>num[i]</code>&nbsp;with&nbsp;<code>change[num[i]]</code>).</p>



<p>Return&nbsp;<em>a string representing the&nbsp;<strong>largest</strong>&nbsp;possible integer after&nbsp;<strong>mutating</strong>&nbsp;(or choosing not to) a&nbsp;<strong>single substring</strong>&nbsp;of&nbsp;</em><code>num</code>.</p>



<p>A&nbsp;<strong>substring</strong>&nbsp;is a contiguous sequence of characters within the string.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = "132", change = [9,8,5,0,3,6,4,2,6,8]
<strong>Output:</strong> "832"
<strong>Explanation:</strong> Replace the substring "1":
- 1 maps to change[1] = 8.
Thus, "132" becomes "832".
"832" is the largest number that can be created, so return it.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = "021", change = [9,4,3,5,7,2,1,9,0,6]
<strong>Output:</strong> "934"
<strong>Explanation:</strong> Replace the substring "021":
- 0 maps to change[0] = 9.
- 2 maps to change[2] = 3.
- 1 maps to change[1] = 4.
Thus, "021" becomes "934".
"934" is the largest number that can be created, so return it.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = "5", change = [1,4,7,5,3,2,5,6,9,4]
<strong>Output:</strong> "5"
<strong>Explanation:</strong> "5" is already the largest number that can be created, so return it.
</pre>



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



<ul><li><code>1 &lt;= num.length &lt;= 10<sup>5</sup></code></li><li><code>num</code>&nbsp;consists of only digits&nbsp;<code>0-9</code>.</li><li><code>change.length == 10</code></li><li><code>0 &lt;= change[d] &lt;= 9</code></li></ul>



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



<p>Find the first digit that is less equal to the mutated one as the start of the substring, keep replacing as long as mutated &gt;= current.</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:
  string maximumNumber(string num, vector&lt;int&gt;&amp; change) {
    const int n = num.size();
    for (int i = 0; i &lt; n; ++i)
      if (num[i] - '0' &lt; change[num[i] - '0']) {
        for (int j = i; j &lt; n &amp;&amp; num[j] - '0' &lt;= change[num[j] - '0']; ++j)
          num[j] = change[num[j] - '0'] + '0';
        break;
      }
    return num;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1946-largest-number-after-mutating-substring/">花花酱 LeetCode 1946. Largest Number After Mutating Substring</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-1946-largest-number-after-mutating-substring/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
