{"id":7648,"date":"2020-11-14T10:50:48","date_gmt":"2020-11-14T18:50:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7648"},"modified":"2020-11-14T10:51:55","modified_gmt":"2020-11-14T18:51:55","slug":"leetcode-1653-minimum-deletions-to-make-string-balanced","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1653-minimum-deletions-to-make-string-balanced\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1653. Minimum Deletions to Make String Balanced"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;consisting only of characters&nbsp;<code>'a'<\/code>&nbsp;and&nbsp;<code>'b'<\/code>\u200b\u200b\u200b\u200b.<\/p>\n\n\n\n<p>You can delete any number of characters in&nbsp;<code>s<\/code>&nbsp;to make&nbsp;<code>s<\/code>&nbsp;<strong>balanced<\/strong>.&nbsp;<code>s<\/code>&nbsp;is&nbsp;<strong>balanced<\/strong>&nbsp;if there is no pair of indices&nbsp;<code>(i,j)<\/code>&nbsp;such that&nbsp;<code>i &lt; j<\/code>&nbsp;and&nbsp;<code>s[i] = 'b'<\/code>&nbsp;and&nbsp;<code>s[j]= 'a'<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of deletions needed to make&nbsp;<\/em><code>s<\/code><em>&nbsp;<strong>balanced<\/strong><\/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 = \"aababbab\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> You can either:\nDelete the characters at 0-indexed positions 2 and 6 (\"aababbab\" -&gt; \"aaabbb\"), or\nDelete the characters at 0-indexed positions 3 and 6 (\"aababbab\" -&gt; \"aabbbb\").\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 = \"bbaaaaabb\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The only solution is to delete the first two characters.\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[i]<\/code>&nbsp;is&nbsp;<code>'a'<\/code>&nbsp;or&nbsp;<code>'b'<\/code>\u200b\u200b.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i][0] := min # of dels to make s[0:i] all &#8216;a&#8217;s;<br>dp[i][1] := min # of dels to make s[0:i] ends with 0 or mode &#8216;b&#8217;s<\/p>\n\n\n\n<p>if s[i-1] == &#8216;a&#8217;: <br>  dp[i + 1][0] = dp[i][0],  dp[i + 1][1] = min(dp[i + 1][0], dp[i][1] + 1)<br>else:<br>  dp[i + 1][0] = dp[i][0] + 1, dp[i + 1][1] = dp[i][1]<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; 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 minimumDeletions(string s) {\n    \/\/ const int n = s.length();\n    \/\/ dp[i][0] := min # of dels to make s[0:i] all 'a's;\n    \/\/ dp[i][1] := min # of dels to make s[0:i] ends with 0+ 'b's\n    \/\/ vector<vector<int>> dp(n + 1, vector<int>(2));\n    \/\/ for (int i = 0; i < n; ++i) {\n    \/\/   if (s[i] == 'a') {\n    \/\/     dp[i + 1][0] = dp[i][0];\n    \/\/     dp[i + 1][1] = min(dp[i + 1][0], dp[i][1] + 1);\n    \/\/   } else {\n    \/\/     dp[i + 1][0] = dp[i][0] + 1;\n    \/\/     dp[i + 1][1] = dp[i][1];\n    \/\/   }\n    \/\/ }\n    \/\/ return min(dp[n][0], dp[n][1]);\n    int a = 0, b = 0;\n    for (char c : s) {\n      if (c == 'a') b = min(a, b + 1); \n      else ++a;\n    }\n    return min(a, b);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;consisting only of characters&nbsp;&#8216;a&#8217;&nbsp;and&nbsp;&#8216;b&#8217;\u200b\u200b\u200b\u200b. You can delete any number of characters in&nbsp;s&nbsp;to make&nbsp;s&nbsp;balanced.&nbsp;s&nbsp;is&nbsp;balanced&nbsp;if there is no pair of indices&nbsp;(i,j)&nbsp;such that&nbsp;i &lt;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[667,18,177,4],"class_list":["post-7648","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-delection","tag-dp","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7648","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=7648"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7648\/revisions"}],"predecessor-version":[{"id":7650,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7648\/revisions\/7650"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}