{"id":9725,"date":"2022-05-09T23:52:40","date_gmt":"2022-05-10T06:52:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9725"},"modified":"2022-05-09T23:53:24","modified_gmt":"2022-05-10T06:53:24","slug":"leetcode-2264-largest-3-same-digit-number-in-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2264-largest-3-same-digit-number-in-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2264. Largest 3-Same-Digit Number in String"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>num<\/code>&nbsp;representing a large integer. An integer is&nbsp;<strong>good<\/strong>&nbsp;if it meets the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It is a&nbsp;<strong>substring<\/strong>&nbsp;of&nbsp;<code>num<\/code>&nbsp;with length&nbsp;<code>3<\/code>.<\/li><li>It consists of only one unique digit.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum good&nbsp;<\/strong>integer as a&nbsp;<strong>string<\/strong>&nbsp;or an empty string&nbsp;<\/em><code>\"\"<\/code><em>&nbsp;if no such integer exists<\/em>.<\/p>\n\n\n\n<p>Note:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters within a string.<\/li><li>There may be&nbsp;<strong>leading zeroes<\/strong>&nbsp;in&nbsp;<code>num<\/code>&nbsp;or a good integer.<\/li><\/ul>\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 = \"6<strong><u>777<\/u><\/strong>133339\"\n<strong>Output:<\/strong> \"777\"\n<strong>Explanation:<\/strong> There are two distinct good integers: \"777\" and \"333\".\n\"777\" is the largest, so we return \"777\".\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 = \"23<strong><u>000<\/u><\/strong>19\"\n<strong>Output:<\/strong> \"000\"\n<strong>Explanation:<\/strong> \"000\" is the only good integer.\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 = \"42352338\"\n<strong>Output:<\/strong> \"\"\n<strong>Explanation:<\/strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= num.length &lt;= 1000<\/code><\/li><li><code>num<\/code>&nbsp;only consists of digits.<\/li><\/ul>\n\n\n\n<p><strong>Solution:<\/strong><\/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 largestGoodInteger(string num) {\n    string ans;\n    for (int i = 0; i < num.size() - 2; ++i) {\n      if (num[i] == num[i + 1] &#038;&#038; num[i] == num[i + 2] &#038;&#038; \n         (ans.empty() || num[i] > ans[0]))\n        ans = num.substr(i, 3);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;num&nbsp;representing a large integer. An integer is&nbsp;good&nbsp;if it meets the following conditions: It is a&nbsp;substring&nbsp;of&nbsp;num&nbsp;with length&nbsp;3. It consists of only one&#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,4],"class_list":["post-9725","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9725","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=9725"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9725\/revisions"}],"predecessor-version":[{"id":9727,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9725\/revisions\/9727"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}