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 '?'
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem is to try every possible character ('a', 'b', or 'c') in place of each question mark. We then check if any adjacent characters are the same after making each replacement. If they are, we discard that possibility and try another.
Here's how the algorithm would work step-by-step:
def replace_question_marks_brute_force(input_string):
string_list = list(input_string)
length_of_string = len(string_list)
def solve(index):
if index == length_of_string:
return ''.join(string_list)
if string_list[index] != '?':
return solve(index + 1)
for char in 'abc':
string_list[index] = char
# Check if the new character causes adjacent repeats
is_valid = True
if index > 0 and string_list[index] == string_list[index - 1]:
is_valid = False
if index < length_of_string - 1 and string_list[index] == string_list[index + 1]:
is_valid = False
if is_valid:
result = solve(index + 1)
if result:
return result
# Backtrack: Reset the character if no solution found
string_list[index] = '?'
return None
return solve(0)
The goal is to fill question marks with letters so that no two adjacent letters are the same. We can solve this by going through the string and, whenever we see a question mark, choosing a letter that's different from its neighbors.
Here's how the algorithm would work step-by-step:
def replace_question_marks(input_string):
string_as_list = list(input_string)
string_length = len(string_as_list)
for index in range(string_length):
if string_as_list[index] == '?':
# Determine neighbors; default to empty string if out of bounds.
previous_character = string_as_list[index - 1] if index > 0 else ''
next_character = string_as_list[index + 1] if index < string_length - 1 else ''
# Find a character that is different from its neighbors.
for char_to_try in 'abc':
if char_to_try != previous_character and char_to_try != next_character:
# Replace the question mark; break since a valid char found.
string_as_list[index] = char_to_try
break
return ''.join(string_as_list)
Case | How to Handle |
---|---|
Null or empty string input | Return an empty string or throw an IllegalArgumentException, depending on requirements |
String with single '?' character | Replace it with 'a' since there are no adjacent characters to consider |
String with two '?' characters at the beginning | Replace the first with 'a' and the second with 'b' to avoid consecutive repeating characters |
String with two '?' characters at the end | Replace the second to last with a char that is not the previous one, and the last with a char that is not the second to last |
String with consecutive '?' characters in the middle | Iterate through and replace each '?' ensuring that it's different from its adjacent characters |
String with '?' character at the beginning followed by a non-'?' character | Replace the '?' with a character that's different from the following character |
String with '?' character at the end preceded by a non-'?' character | Replace the '?' with a character that's different from the preceding character |
Long string with many '?' characters, performance considerations | An iterative approach is generally more efficient than recursion and avoids stack overflow errors with long inputs. |