{"id":7222,"date":"2020-08-10T00:34:23","date_gmt":"2020-08-10T07:34:23","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7222"},"modified":"2020-08-10T21:34:26","modified_gmt":"2020-08-11T04:34:26","slug":"leetcode-1542-find-longest-awesome-substring","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1542-find-longest-awesome-substring\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1542. Find Longest Awesome Substring"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1542. Find Longest Awesome Substring - \u5237\u9898\u627e\u5de5\u4f5c EP349\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/b0oxAd94FOg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>. An&nbsp;<em>awesome<\/em>&nbsp;substring is a non-empty substring of&nbsp;<code>s<\/code>&nbsp;such that we can make any number of swaps in order to make it palindrome.<\/p>\n\n\n\n<p>Return the length of the maximum length&nbsp;<strong>awesome substring<\/strong>&nbsp;of&nbsp;<code>s<\/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> s = \"3242415\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> \"24241\" is the longest awesome substring, we can form the palindrome \"24142\" with some swaps.\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 = \"12345678\"\n<strong>Output:<\/strong> 1\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 = \"213123\"\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> \"213123\" is the longest awesome substring, we can form the palindrome \"231132\" with some swaps.\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 = \"00\"\n<strong>Output:<\/strong> 2\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;= 10^5<\/code><\/li><li><code>s<\/code>&nbsp;consists only of digits.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix mask + Hashtable<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1542-ep349.png\" alt=\"\" class=\"wp-image-7233\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1542-ep349.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1542-ep349-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1542-ep349-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1542-ep349-2.png\" alt=\"\" class=\"wp-image-7234\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1542-ep349-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1542-ep349-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/08\/1542-ep349-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>For a palindrome all digits must occurred even times expect one. We can use a 10 bit mask to track the occurrence of each digit for prefix s[0~i]. 0 is even, 1 is odd.<\/p>\n\n\n\n<p>We use a hashtable to track the <strong>first index<\/strong> of each prefix state.<br>If s[0~i] and s[0~j] have the same state which means every digits in s[i+1~j] occurred even times (zero is also even) and it&#8217;s an awesome string. Then (j &#8211; (i+1)  + 1) = j &#8211; i is the length of the palindrome. So far so good.<\/p>\n\n\n\n<p>But we still need to consider the case when there is a digit with odd occurrence. We can enumerate all possible ones from 0 to 9, and temporarily flip the bit of the digit and see whether that state happened before. <\/p>\n\n\n\n<p>fisrt_index[0] = -1, first_index[*] = inf<br>ans = max(ans, j &#8211; first_index[mask])<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(2^10) = 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++\">\nclass Solution {\npublic:\n  int longestAwesome(string s) {\n    constexpr int kInf = 1e9;\n    vector<int> idx(1024, kInf);\n    idx[0] = -1;\n    int mask = 0; \/\/ prefix's state 0:even, 1:odd\n    int ans = 0;\n    for (int i = 0; i < s.length(); ++i) {\n      mask ^= (1 << (s[i] - '0'));\n      ans = max(ans, i - idx[mask]);\n      \/\/ One digit with odd count is allowed.\n      for (int j = 0; j < 10; ++j)\n        ans = max(ans, i - idx[mask ^ (1 << j)]);\n      idx[mask] = min(idx[mask], i);\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\n\/\/ Author: Huahua\nclass Solution {\n  public int longestAwesome(String s) {\n    final int n = s.length();\n    int[] idx = new int[1024];\n    Arrays.fill(idx, n);\n    idx[0] = -1;\n    int mask = 0;\n    int ans = 0;\n    for (int i = 0; i < n; ++i) {\n      mask ^= (1 << (s.charAt(i) - '0'));\n      ans = Math.max(ans, i - idx[mask]);      \n      for (int j = 0; j < 10; ++j)\n        ans = Math.max(ans, \n                       i - idx[mask ^ (1 << j)]);\n      idx[mask] = Math.min(idx[mask], i);\n    }\n    return ans;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def longestAwesome(self, s: str) -> int:  \n    idx = [-1] + [len(s)] * 1023\n    ans, mask = 0, 0\n    for i, c in enumerate(s):\n      mask ^= 1 << (ord(c) - ord('0'))\n      ans = max([ans, i - idx[mask]]\n                + [i - idx[mask ^ (1 << j)] for j in range(10)])\n      idx[mask] = min(idx[mask], i)\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s. An&nbsp;awesome&nbsp;substring is a non-empty substring of&nbsp;s&nbsp;such that we can make any number of swaps in order to make it palindrome. Return the&#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":[621,82,97,63],"class_list":["post-7222","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-bitmask","tag-hashtable","tag-prefix","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7222","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=7222"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7222\/revisions"}],"predecessor-version":[{"id":7238,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7222\/revisions\/7238"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}