{"id":8595,"date":"2021-10-04T19:54:10","date_gmt":"2021-10-05T02:54:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8595"},"modified":"2021-10-04T20:14:05","modified_gmt":"2021-10-05T03:14:05","slug":"leetcode-2027-minimum-moves-to-convert-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2027-minimum-moves-to-convert-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2027. Minimum Moves to Convert String"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;consisting of&nbsp;<code>n<\/code>&nbsp;characters which are either&nbsp;<code>'X'<\/code>&nbsp;or&nbsp;<code>'O'<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>move<\/strong>&nbsp;is defined as selecting&nbsp;<strong>three<\/strong>&nbsp;<strong>consecutive characters<\/strong>&nbsp;of&nbsp;<code>s<\/code>&nbsp;and converting them to&nbsp;<code>'O'<\/code>. Note that if a move is applied to the character&nbsp;<code>'O'<\/code>, it will stay the&nbsp;<strong>same<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of moves required so that all the characters of&nbsp;<\/em><code>s<\/code><em>&nbsp;are converted to&nbsp;<\/em><code>'O'<\/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 = \"XXX\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> XXX -&gt; OOO\nWe select all the 3 characters and convert them in one move.\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 = \"XXOX\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> XXOX -&gt; OOOX -&gt; OOOO\nWe select the first 3 characters in the first move, and convert them to <code>'O'<\/code>.\nThen we select the last 3 characters and convert them so that the final string contains all <code>'O'<\/code>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 = \"OOOO\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no <code>'X's<\/code> in <code>s<\/code> to convert.\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;= 1000<\/code><\/li><li><code>s[i]<\/code>&nbsp;is either&nbsp;<code>'X'<\/code>&nbsp;or&nbsp;<code>'O'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Straight Forward<\/strong><\/h2>\n\n\n\n<p>if s[i] == &#8216;X&#8217;, change s[i], s[i + 1] and s[i + 2] to &#8216;O&#8217;.<\/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++\">\nclass Solution {\npublic:\n  int minimumMoves(string s) {\n    const int n = s.length();\n    int ans = 0;\n    for (size_t i = 0; i < n; ++i) {\n      if (s[i] == 'X') {\n        ans += 1;\n        i += 2;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;consisting of&nbsp;n&nbsp;characters which are either&nbsp;&#8216;X&#8217;&nbsp;or&nbsp;&#8216;O&#8217;. A&nbsp;move&nbsp;is defined as selecting&nbsp;three&nbsp;consecutive characters&nbsp;of&nbsp;s&nbsp;and converting them to&nbsp;&#8216;O&#8217;. Note that if a move is applied to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,47],"tags":[222,722,4],"class_list":["post-8595","post","type-post","status-publish","format-standard","hentry","category-array","category-string","tag-easy","tag-moves","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8595","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=8595"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8595\/revisions"}],"predecessor-version":[{"id":8597,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8595\/revisions\/8597"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}