{"id":6142,"date":"2020-01-26T10:11:49","date_gmt":"2020-01-26T18:11:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6142"},"modified":"2020-01-26T10:14:37","modified_gmt":"2020-01-26T18:14:37","slug":"leetcode-1332-remove-palindromic-subsequences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1332-remove-palindromic-subsequences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1332. Remove Palindromic Subsequences"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;consisting only of&nbsp;letters&nbsp;<code>'a'<\/code>&nbsp;and&nbsp;<code>'b'<\/code>. In a single step you can remove one&nbsp;palindromic&nbsp;<strong>subsequence<\/strong>&nbsp;from&nbsp;<code>s<\/code>.<\/p>\n\n\n\n<p>Return the minimum number of steps to make the given string empty.<\/p>\n\n\n\n<p>A string is a subsequence of a given string, if it is generated by deleting some characters of a given string without changing its order.<\/p>\n\n\n\n<p>A string is called palindrome if is one that reads the same backward as well as forward.<\/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 = \"ababa\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> String is already palindrome\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 = \"abb\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \"<strong>a<\/strong>bb\" -&gt; \"<strong>bb<\/strong>\" -&gt; \"\". \nRemove palindromic subsequence \"a\" then \"bb\".\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 = \"baabb\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \"<strong>baa<\/strong>b<strong>b<\/strong>\" -&gt; \"b\" -&gt; \"\". \nRemove palindromic subsequence \"baab\" then \"b\".\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 = \"\"\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= s.length &lt;= 1000<\/code><\/li><li><code>s<\/code>&nbsp;only consists of letters &#8216;a&#8217; and &#8216;b&#8217;<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>if s is empty => 0 step<br>if s is a palindrome => 1 step<br>Otherwise, 2 steps&#8230;<br>1. delete all the as<br>2. delete all the bs<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) \/ 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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int removePalindromeSub(string s) {\n    if (s.empty()) return 0;\n    if (s == string(rbegin(s), rend(s))) return 1;\n    return 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;consisting only of&nbsp;letters&nbsp;&#8216;a&#8217;&nbsp;and&nbsp;&#8216;b&#8217;. In a single step you can remove one&nbsp;palindromic&nbsp;subsequence&nbsp;from&nbsp;s. Return the minimum number of steps to make the given string empty.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,31,376,95,229],"class_list":["post-6142","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-math","tag-on","tag-palindrome","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6142","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=6142"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6142\/revisions"}],"predecessor-version":[{"id":6145,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6142\/revisions\/6145"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}