{"id":7345,"date":"2020-09-05T21:42:45","date_gmt":"2020-09-06T04:42:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7345"},"modified":"2020-09-05T21:42:55","modified_gmt":"2020-09-06T04:42:55","slug":"leetcode-1576-replace-all-s-to-avoid-consecutive-repeating-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1576-replace-all-s-to-avoid-consecutive-repeating-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1576. Replace All ?&#8217;s to Avoid Consecutive Repeating Characters"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;containing only lower case English letters&nbsp;and the &#8216;?&#8217;&nbsp;character, convert&nbsp;<strong>all&nbsp;<\/strong>the &#8216;?&#8217; characters into lower case letters such that the final string does not contain any&nbsp;<strong>consecutive repeating&nbsp;<\/strong>characters.&nbsp;You&nbsp;<strong>cannot&nbsp;<\/strong>modify the non &#8216;?&#8217; characters.<\/p>\n\n\n\n<p>It is&nbsp;<strong>guaranteed&nbsp;<\/strong>that there are no consecutive repeating characters in the given string&nbsp;<strong>except&nbsp;<\/strong>for &#8216;?&#8217;.<\/p>\n\n\n\n<p>Return the final string after all the conversions (possibly zero) have been made. If there is more than one solution, return any of them.&nbsp;It can be shown that an answer is always possible with the given constraints.<\/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 = \"?zs\"\n<strong>Output:<\/strong> \"azs\"\n<strong>Explanation<\/strong>: There are 25 solutions for this problem. From \"azs\" to \"yzs\", all are valid. Only \"z\" is an invalid modification as the string will consist of consecutive repeating characters in \"zzs\".<\/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 = \"ubv?w\"\n<strong>Output:<\/strong> \"ubvaw\"\n<strong>Explanation<\/strong>: There are 24 solutions for this problem. Only \"v\" and \"w\" are invalid modifications as the strings will consist of consecutive repeating characters in \"ubvvw\" and \"ubvww\".\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 = \"j?qg??b\"\n<strong>Output:<\/strong> \"jaqgacb\"\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 = \"??yw?ipkj?\"\n<strong>Output:<\/strong> \"acywaipkja\"\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&nbsp;&lt;= 100<\/code><\/li><li><code>s<\/code>&nbsp;contains&nbsp;only lower case English letters and &#8216;?&#8217;.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>For each ?, find the first one among &#8216;abc&#8217; that is not same as left or right.<\/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  string modifyString(string s) {  \n    const int n = s.length();\n    for (int i = 0; i < n; ++i) {\n      if (s[i] != '?') continue;\n      for (char c : \"abc\")\n        if ((i == 0 || s[i - 1] != c) &#038;&#038; (i == n - 1 || s[i + 1] != c)) {\n          s[i] = c;\n          break;\n        }\n    }\n    return s;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;containing only lower case English letters&nbsp;and the &#8216;?&#8217;&nbsp;character, convert&nbsp;all&nbsp;the &#8216;?&#8217; characters into lower case letters such that the final string does not contain&#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":[647,222,4],"class_list":["post-7345","post","type-post","status-publish","format-standard","hentry","category-string","tag-constructive","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7345","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=7345"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7345\/revisions"}],"predecessor-version":[{"id":7347,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7345\/revisions\/7347"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}