{"id":8579,"date":"2021-08-15T11:58:26","date_gmt":"2021-08-15T18:58:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8579"},"modified":"2021-08-15T11:59:55","modified_gmt":"2021-08-15T18:59:55","slug":"leetcode-1903-largest-odd-number-in-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1903-largest-odd-number-in-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1903. Largest Odd Number in String"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>num<\/code>, representing a large integer. Return&nbsp;<em>the&nbsp;<strong>largest-valued odd<\/strong>&nbsp;integer (as a string) that is a&nbsp;<strong>non-empty substring<\/strong>&nbsp;of&nbsp;<\/em><code>num<\/code><em>, or an empty string&nbsp;<\/em><code>\"\"<\/code><em>&nbsp;if no odd integer exists<\/em>.<\/p>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters within a 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 = \"52\"\n<strong>Output:<\/strong> \"5\"\n<strong>Explanation:<\/strong> The only non-empty substrings are \"5\", \"2\", and \"52\". \"5\" is the only odd number.\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 = \"4206\"\n<strong>Output:<\/strong> \"\"\n<strong>Explanation:<\/strong> There are no odd numbers in \"4206\".\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 = \"35427\"\n<strong>Output:<\/strong> \"35427\"\n<strong>Explanation:<\/strong> \"35427\" is already an odd number.\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;only consists of digits and does not contain any leading zeros.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Find right most odd digit<\/strong><\/h2>\n\n\n\n<p>We just need to find the right most digit that is odd, answer will be num[0:r]. <\/p>\n\n\n\n<p>Answer must start with num[0]. <br>Proof:<br>Assume the largest number is num[i:r] i &gt; 0, we can always extend to the left, e.g. num[i-1:r] which is also an  odd number and it&#8217;s larger than num[i:r] which contradicts our assumption. Thus the largest odd number (if exists) must start with num[0].<\/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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  string largestOddNumber(string num) {\n    for (int i = num.length() - 1; i >= 0; --i)\n      if ((num[i] - '0') & 1) return num.substr(0, i + 1);\n    return \"\";\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;num, representing a large integer. Return&nbsp;the&nbsp;largest-valued odd&nbsp;integer (as a string) that is a&nbsp;non-empty substring&nbsp;of&nbsp;num, or an empty string&nbsp;&#8220;&#8221;&nbsp;if no odd integer&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,31,720,4],"class_list":["post-8579","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-math","tag-proof","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8579","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=8579"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8579\/revisions"}],"predecessor-version":[{"id":8583,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8579\/revisions\/8583"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}