1576. Replace All ?’s to Avoid Consecutive Repeating Characters LeetCode Solution

In this guide, you will get 1576. Replace All ?’s to Avoid Consecutive Repeating Characters LeetCode Solution with the best time and space complexity. The solution to Replace All ?’s to Avoid Consecutive Repeating Characters problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Replace All ?’s to Avoid Consecutive Repeating Characters solution in C++
  4. Replace All ?’s to Avoid Consecutive Repeating Characters solution in Java
  5. Replace All ?’s to Avoid Consecutive Repeating Characters solution in Python
  6. Additional Resources
1576. Replace All ?'s to Avoid Consecutive Repeating Characters LeetCode Solution image

Problem Statement of Replace All ?’s to Avoid Consecutive Repeating Characters

Given a string s containing only lowercase English letters and the ‘?’ character, convert all the ‘?’ characters into lowercase letters such that the final string does not contain any consecutive repeating characters. You cannot modify the non ‘?’ characters.
It is guaranteed that there are no consecutive repeating characters in the given string except for ‘?’.
Return the final string after all the conversions (possibly zero) have been made. If there is more than one solution, return any of them. It can be shown that an answer is always possible with the given constraints.

Example 1:

Input: s = “?zs”
Output: “azs”
Explanation: 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”.

Example 2:

Input: s = “ubv?w”
Output: “ubvaw”
Explanation: 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”.

Constraints:

1 <= s.length <= 100
s consist of lowercase English letters and '?'.

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1576. Replace All ?’s to Avoid Consecutive Repeating Characters LeetCode Solution in C++

class Solution {
 public:
  string modifyString(string s) {
    string ans;

    for (int i = 0; i < s.length(); ++i)
      if (s[i] == '?')
        ans += nextAvailable(ans, s, i);
      else
        ans += s[i];

    return ans;
  }

 private:
  char nextAvailable(const string& ans, const string& s, int i) {
    char c = 'a';
    while ((i > 0 && ans[i - 1] == c) ||  //
           (i + 1 < s.size() && c == s[i + 1]))
      ++c;
    return c;
  }
};
/* code provided by PROGIEZ */

1576. Replace All ?’s to Avoid Consecutive Repeating Characters LeetCode Solution in Java

class Solution {
  public String modifyString(String s) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < s.length(); ++i)
      if (s.charAt(i) == '?')
        sb.append(nextAvailable(sb, s, i));
      else
        sb.append(s.charAt(i));

    return sb.toString();
  }

  private char nextAvailable(StringBuilder sb, final String s, int i) {
    char c = 'a';
    while ((i > 0 && sb.charAt(i - 1) == c) || (i + 1 < s.length() && c == s.charAt(i + 1)))
      ++c;
    return c;
  }
}
// code provided by PROGIEZ

1576. Replace All ?’s to Avoid Consecutive Repeating Characters LeetCode Solution in Python

class Solution:
  def modifyString(self, s: str) -> str:
    ans = []

    def nextAvailable(ans: list[int], s: str, i: int) -> str:
      c = 'a'
      while ((i > 0 and ans[i - 1] == c) or
             (i + 1 < len(s) and c == s[i + 1])):
        c = chr(ord(c) + 1)
      return c

    for i, c in enumerate(s):
      if c == '?':
        ans.append(nextAvailable(ans, s, i))
      else:
        ans.append(c)

    return ''.join(ans)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.