{"id":9716,"date":"2022-05-03T17:41:47","date_gmt":"2022-05-04T00:41:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9716"},"modified":"2022-05-03T17:54:59","modified_gmt":"2022-05-04T00:54:59","slug":"leetcode-2259-remove-digit-from-number-to-maximize-result","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-2259-remove-digit-from-number-to-maximize-result\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2259. Remove Digit From Number to Maximize Result"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>number<\/code>&nbsp;representing a&nbsp;<strong>positive integer<\/strong>&nbsp;and a character&nbsp;<code>digit<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the resulting string after removing&nbsp;<strong>exactly one occurrence<\/strong>&nbsp;of&nbsp;<\/em><code>digit<\/code><em>&nbsp;from&nbsp;<\/em><code>number<\/code><em>&nbsp;such that the value of the resulting string in&nbsp;<strong>decimal<\/strong>&nbsp;form is&nbsp;<strong>maximized<\/strong><\/em>. The test cases are generated such that&nbsp;<code>digit<\/code>&nbsp;occurs at least once in&nbsp;<code>number<\/code>.<\/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> number = \"123\", digit = \"3\"\n<strong>Output:<\/strong> \"12\"\n<strong>Explanation:<\/strong> There is only one '3' in \"123\". After removing '3', the result is \"12\".\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> number = \"1231\", digit = \"1\"\n<strong>Output:<\/strong> \"231\"\n<strong>Explanation:<\/strong> We can remove the first '1' to get \"231\" or remove the second '1' to get \"123\".\nSince 231 &gt; 123, we return \"231\".\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> number = \"551\", digit = \"5\"\n<strong>Output:<\/strong> \"51\"\n<strong>Explanation:<\/strong> We can remove either the first or second '5' from \"551\".\nBoth result in the string \"51\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= number.length &lt;= 100<\/code><\/li><li><code>number<\/code>&nbsp;consists of digits from&nbsp;<code>'1'<\/code>&nbsp;to&nbsp;<code>'9'<\/code>.<\/li><li><code>digit<\/code>&nbsp;is a digit from&nbsp;<code>'1'<\/code>&nbsp;to&nbsp;<code>'9'<\/code>.<\/li><li><code>digit<\/code>&nbsp;occurs at least once in&nbsp;<code>number<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Try all possible resulting strings.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(n)<\/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 removeDigit(string number, char digit) {\n    const int n = number.size();\n    string ans(number.size() - 1, '1');\n    for (int i = 0; i < n; ++i)\n      if (number[i] == digit)\n        ans = max(ans, number.substr(0, i) + number.substr(i + 1));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;number&nbsp;representing a&nbsp;positive integer&nbsp;and a character&nbsp;digit. Return&nbsp;the resulting string after removing&nbsp;exactly one occurrence&nbsp;of&nbsp;digit&nbsp;from&nbsp;number&nbsp;such that the value of the resulting string in&nbsp;decimal&nbsp;form is&nbsp;maximized.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[222,4],"class_list":["post-9716","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9716","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=9716"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9716\/revisions"}],"predecessor-version":[{"id":9721,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9716\/revisions\/9721"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}