{"id":8068,"date":"2021-02-06T08:30:29","date_gmt":"2021-02-06T16:30:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8068"},"modified":"2021-02-06T08:30:56","modified_gmt":"2021-02-06T16:30:56","slug":"leetcode-1750-minimum-length-of-string-after-deleting-similar-ends","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1750-minimum-length-of-string-after-deleting-similar-ends\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1750. Minimum Length of String After Deleting Similar Ends"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;consisting only of characters&nbsp;<code>'a'<\/code>,&nbsp;<code>'b'<\/code>, and&nbsp;<code>'c'<\/code>. You are asked to apply the following algorithm on the string any number of times:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Pick a&nbsp;<strong>non-empty<\/strong>&nbsp;prefix from the string&nbsp;<code>s<\/code>&nbsp;where all the characters in the prefix are equal.<\/li><li>Pick a&nbsp;<strong>non-empty<\/strong>&nbsp;suffix from the string&nbsp;<code>s<\/code>&nbsp;where all the characters in this suffix are equal.<\/li><li>The prefix and the suffix should not intersect at any index.<\/li><li>The characters from the prefix and suffix must be the same.<\/li><li>Delete both the prefix and the suffix.<\/li><\/ol>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum length<\/strong>&nbsp;of&nbsp;<\/em><code>s<\/code>&nbsp;<em>after performing the above operation any number of times (possibly zero times)<\/em>.<\/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 = \"ca\"\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>You can't remove any characters, so the string stays as is.\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 = \"cabaabac\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> An optimal sequence of operations is:\n- Take prefix = \"c\" and suffix = \"c\" and remove them, s = \"abaaba\".\n- Take prefix = \"a\" and suffix = \"a\" and remove them, s = \"baab\".\n- Take prefix = \"b\" and suffix = \"b\" and remove them, s = \"aa\".\n- Take prefix = \"a\" and suffix = \"a\" and remove them, s = \"\".<\/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 = \"aabccabba\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> An optimal sequence of operations is:\n- Take prefix = \"aa\" and suffix = \"a\" and remove them, s = \"bccabb\".\n- Take prefix = \"b\" and suffix = \"bb\" and remove them, s = \"cca\".\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<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;only consists of characters&nbsp;<code>'a'<\/code>,&nbsp;<code>'b'<\/code>, and&nbsp;<code>'c'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two Pointers + Greedy<\/strong><\/h2>\n\n\n\n<p>Delete all the chars for each prefix and suffix pair. <\/p>\n\n\n\n<p>Time complexity: O(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 minimumLength(string s) {\n    int l = 0, r = s.length() - 1;\n    while (l < r) {\n      if (s[l] != s[r]) break;\n      const char c = s[l];\n      while (l <= r &#038;&#038; s[l] == c) ++l;\n      while (l <= r &#038;&#038; s[r] == c) --r;\n    }\n    return r - l + 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;consisting only of characters&nbsp;&#8216;a&#8217;,&nbsp;&#8216;b&#8217;, and&nbsp;&#8216;c&#8217;. You are asked to apply the following algorithm on the string any number of times: Pick a&nbsp;non-empty&nbsp;prefix from&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177,4],"class_list":["post-8068","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8068","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=8068"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8068\/revisions"}],"predecessor-version":[{"id":8070,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8068\/revisions\/8070"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}