{"id":7525,"date":"2020-10-18T10:39:37","date_gmt":"2020-10-18T17:39:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7525"},"modified":"2020-10-18T10:40:30","modified_gmt":"2020-10-18T17:40:30","slug":"leetcode-1624-largest-substring-between-two-equal-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1624-largest-substring-between-two-equal-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1624. Largest Substring Between Two Equal Characters"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>, return&nbsp;<em>the length of the longest substring between two equal characters, excluding the two characters.<\/em>&nbsp;If there is no such substring return&nbsp;<code>-1<\/code>.<\/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> s = \"aa\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The optimal substring here is an empty substring between the two <code>'a's<\/code>.<\/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 = \"abca\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The optimal substring here is \"bc\".\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> s = \"cbzxy\"\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There are no characters that appear twice in s.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"cabbac\"\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The optimal substring here is \"abba\". Other non-optimal substrings include \"bb\" and \"\".\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;= 300<\/code><\/li><li><code>s<\/code>&nbsp;contains only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Remember the first position each letter occurs.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(26) <\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int maxLengthBetweenEqualCharacters(string s) {\n    vector<int> first(26, -1);\n    int ans = -1;\n    for (int i = 0; i < s.length(); ++i) {\n      int&#038; p = first[s[i] - 'a'];\n      if (p != -1) {\n        ans = max(ans, i - p - 1);\n      } else {\n        p = i;\n      }      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, return&nbsp;the length of the longest substring between two equal characters, excluding the two characters.&nbsp;If there is no such substring return&nbsp;-1. A&nbsp;substring&nbsp;is a&#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-7525","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\/7525","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=7525"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7525\/revisions"}],"predecessor-version":[{"id":7527,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7525\/revisions\/7527"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}