{"id":9314,"date":"2021-12-31T12:32:39","date_gmt":"2021-12-31T20:32:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9314"},"modified":"2021-12-31T12:33:45","modified_gmt":"2021-12-31T20:33:45","slug":"leetcode-1946-largest-number-after-mutating-substring","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1946-largest-number-after-mutating-substring\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1946. Largest Number After Mutating Substring"},"content":{"rendered":"\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters within the string.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> num = \"132\", change = [9,8,5,0,3,6,4,2,6,8]\n<strong>Output:<\/strong> \"832\"\n<strong>Explanation:<\/strong> Replace the substring \"1\":\n- 1 maps to change[1] = 8.\nThus, \"132\" becomes \"832\".\n\"832\" is the largest number that can be created, so return it.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> num = \"021\", change = [9,4,3,5,7,2,1,9,0,6]\n<strong>Output:<\/strong> \"934\"\n<strong>Explanation:<\/strong> Replace the substring \"021\":\n- 0 maps to change[0] = 9.\n- 2 maps to change[2] = 3.\n- 1 maps to change[1] = 4.\nThus, \"021\" becomes \"934\".\n\"934\" is the largest number that can be created, so return it.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> num = \"5\", change = [1,4,7,5,3,2,5,6,9,4]\n<strong>Output:<\/strong> \"5\"\n<strong>Explanation:<\/strong> \"5\" is already the largest number that can be created, so return it.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string maximumNumber(string num, vector<int>& change) {\n    const int n = num.size();\n    for (int i = 0; i < n; ++i)\n      if (num[i] - '0' < change[num[i] - '0']) {\n        for (int j = i; j < n &#038;&#038; num[j] - '0' <= change[num[j] - '0']; ++j)\n          num[j] = change[num[j] - '0'] + '0';\n        break;\n      }\n    return num;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;num, which represents a large integer. You are also given a&nbsp;0-indexed&nbsp;integer array&nbsp;change&nbsp;of length&nbsp;10&nbsp;that maps each digit&nbsp;0-9&nbsp;to another digit. More formally, digit&nbsp;d&nbsp;maps&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177,753,314],"class_list":["post-9314","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-replacing","tag-substring","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9314"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9314\/revisions"}],"predecessor-version":[{"id":9316,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9314\/revisions\/9316"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}