{"id":8249,"date":"2021-03-20T09:23:58","date_gmt":"2021-03-20T16:23:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8249"},"modified":"2021-03-20T09:24:41","modified_gmt":"2021-03-20T16:24:41","slug":"leetcode-1796-second-largest-digit-in-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1796-second-largest-digit-in-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1796. Second Largest Digit in a String"},"content":{"rendered":"\n<p>Given an alphanumeric string&nbsp;<code>s<\/code>, return&nbsp;<em>the&nbsp;<strong>second largest<\/strong>&nbsp;numerical digit that appears in&nbsp;<\/em><code>s<\/code><em>, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if it does not exist<\/em>.<\/p>\n\n\n\n<p>An&nbsp;<strong>alphanumeric<\/strong>string is a string consisting of lowercase English letters and digits.<\/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> s = \"dfa12321afd\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The digits that appear in s are [1, 2, 3]. The second largest digit is 2.\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> s = \"abc1111\"\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> The digits that appear in s are [1]. There is no second largest digit. \n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 500<\/code><\/li><li><code>s<\/code>&nbsp;consists of only lowercase English letters and\/or digits.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(10)<\/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  int secondHighest(string s) {\n    vector<int> d(10);\n    for (char c : s)\n      if (c >= '0' && c <= '9')\n        d[c - '0'] = 1;\n    int order = 0;\n    for (int i = 9; i >= 0; --i)\n      if (d[i] && ++order == 2)\n        return i;\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an alphanumeric string&nbsp;s, return&nbsp;the&nbsp;second largest&nbsp;numerical digit that appears in&nbsp;s, or&nbsp;-1&nbsp;if it does not exist. An&nbsp;alphanumericstring is a string consisting of lowercase English letters and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[222,82,4],"class_list":["post-8249","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8249","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=8249"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8249\/revisions"}],"predecessor-version":[{"id":8251,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8249\/revisions\/8251"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}