Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Replace All ?'s to Avoid Consecutive Repeating Characters

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1576" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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 '?'.

Explanation

Here's the solution:

  • Iterate and Replace: Iterate through the string, and whenever a '?' is encountered, replace it with a suitable lowercase letter.
  • Avoid Repetition: When replacing a '?', choose a letter that is different from its adjacent characters (if they exist). Prioritize 'a', 'b', 'c' in this order to find the suitable character.

  • Runtime Complexity: O(n), where n is the length of the string.

  • Storage Complexity: O(n) because we convert the string to list of chars to solve in place.

Code

    def modifyString(s: str) -> str:
    """
    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 '?'.
    """

    s_list = list(s)
    n = len(s_list)

    for i in range(n):
        if s_list[i] == '?':
            for char in 'abc':
                if (i > 0 and s_list[i - 1] == char) or \
                   (i < n - 1 and s_list[i + 1] != '?' and s_list[i + 1] == char):
                    continue
                s_list[i] = char
                break

    return "".join(s_list)

More from this blog

C

Chatmagic blog

2894 posts