{"id":9293,"date":"2021-12-30T22:28:26","date_gmt":"2021-12-31T06:28:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9293"},"modified":"2021-12-31T03:27:59","modified_gmt":"2021-12-31T11:27:59","slug":"leetcode-1930-unique-length-3-palindromic-subsequences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1930-unique-length-3-palindromic-subsequences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1930. Unique Length-3 Palindromic Subsequences"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>, return&nbsp;<em>the number of&nbsp;<strong>unique palindromes of length three<\/strong>&nbsp;that are a&nbsp;<strong>subsequence<\/strong>&nbsp;of&nbsp;<\/em><code>s<\/code>.<\/p>\n\n\n\n<p>Note that even if there are multiple ways to obtain the same subsequence, it is still only counted&nbsp;<strong>once<\/strong>.<\/p>\n\n\n\n<p>A&nbsp;<strong>palindrome<\/strong>&nbsp;is a string that reads the same forwards and backwards.<\/p>\n\n\n\n<p>A&nbsp;<strong>subsequence<\/strong>&nbsp;of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example,&nbsp;<code>\"ace\"<\/code>&nbsp;is a subsequence of&nbsp;<code>\"<u>a<\/u>b<u>c<\/u>d<u>e<\/u>\"<\/code>.<\/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> s = \"aabca\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The 3 palindromic subsequences of length 3 are:\n- \"aba\" (subsequence of \"aabca\")\n- \"aaa\" (subsequence of \"aabca\")\n- \"aca\" (subsequence of \"aabca\")\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 = \"adc\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no palindromic subsequences of length 3 in \"adc\".\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 = \"bbcbaba\"\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The 4 palindromic subsequences of length 3 are:\n- \"bbb\" (subsequence of \"bbcbaba\")\n- \"bcb\" (subsequence of \"bbcbaba\")\n- \"bab\" (subsequence of \"bbcbaba\")\n- \"aba\" (subsequence of \"bbcbaba\")\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= s.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;consists of only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Enumerate first <meta charset=\"utf-8\">character of a palindrome<\/strong><\/h2>\n\n\n\n<p>For a length 3 palindrome, we just need to enumerate the first character c.<br>We found the first and last occurrence of c in original string and scan the middle part to see how many unique characters there.<br><br>e.g. <meta charset=\"utf-8\">aabca<br>Enumerate from a to z, looking for a*a, b*b, &#8230;, z*z.<br>For a*a, <meta charset=\"utf-8\"><span style=\"text-decoration: underline;\"><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">a<\/span><\/strong><\/span><span class=\"has-inline-color has-vivid-red-color\"><strong>abc<\/strong><\/span><span style=\"text-decoration: underline;\"><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>a<\/strong><\/span><\/span>, we found first and last a, in between is <meta charset=\"utf-8\"><span class=\"has-inline-color has-vivid-red-color\"><strong>abc<\/strong><\/span>, which has 3 unique letters. <br>We can use a hastable or a bitset to track unique letters.<\/p>\n\n\n\n<p>Time complexity: O(26*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  int countPalindromicSubsequence(string s) {\n    auto count = [&](char c) -> int {\n      int l = s.find_first_of(c);\n      int r = s.find_last_of(c);\n      if (l == string::npos || r == string::npos) return 0;\n      bitset<26> m;\n      for (int i = l + 1; i < r; ++i)\n        m.set(s[i] - 'a');\n      return m.count();\n    };\n    int ans = 0;\n    for (char c = 'a'; c <= 'z'; ++c)\n      ans += count(c);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, return&nbsp;the number of&nbsp;unique palindromes of length three&nbsp;that are a&nbsp;subsequence&nbsp;of&nbsp;s. Note that even if there are multiple ways to obtain the same subsequence,&#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":[232,82,177,95,4,229],"class_list":["post-9293","post","type-post","status-publish","format-standard","hentry","category-string","tag-bitset","tag-hashtable","tag-medium","tag-palindrome","tag-string","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9293","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=9293"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9293\/revisions"}],"predecessor-version":[{"id":9297,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9293\/revisions\/9297"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}