You are given a string s. s[i] is either a lowercase English letter or '?'.
For a string t having length m containing only lowercase English letters, we define the function cost(i) for an index i as the number of characters equal to t[i] that appeared before it, i.e. in the range [0, i - 1].
The value of t is the sum of cost(i) for all indices i.
For example, for the string t = "aab":
cost(0) = 0cost(1) = 1cost(2) = 0"aab" is 0 + 1 + 0 = 1.Your task is to replace all occurrences of '?' in s with any lowercase English letter so that the value of s is minimized.
Return a string denoting the modified string with replaced occurrences of '?'. If there are multiple strings resulting in the minimum value, return the lexicographically smallest one.
Example 1:
Input: s = "???"
Output: "abc"
Explanation: In this example, we can replace the occurrences of '?' to make s equal to "abc".
For "abc", cost(0) = 0, cost(1) = 0, and cost(2) = 0.
The value of "abc" is 0.
Some other modifications of s that have a value of 0 are "cba", "abz", and, "hey".
Among all of them, we choose the lexicographically smallest.
Example 2:
Input: s = "a?a?"
Output: "abac"
Explanation: In this example, the occurrences of '?' can be replaced to make s equal to "abac".
For "abac", cost(0) = 0, cost(1) = 0, cost(2) = 1, and cost(3) = 0.
The value of "abac" is 1.
Constraints:
1 <= s.length <= 105s[i] is either a lowercase English letter or '?'.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 strategy for this problem involves checking every possible combination of letters that could replace the question marks. It's like trying every possible word to see which one fits best. We generate each possible string and then pick the one that gives us the minimum "value" according to some comparison rule.
Here's how the algorithm would work step-by-step:
def replace_question_marks_brute_force(input_string):
question_mark_indices = [i for i, char in enumerate(input_string) if char == '?']
number_of_question_marks = len(question_mark_indices)
if number_of_question_marks == 0:
return input_string
best_string = None
minimum_string_value = float('inf')
# Iterate through all possible combinations of replacements.
def generate_combinations(current_index, current_string):
nonlocal best_string, minimum_string_value
if current_index == number_of_question_marks:
string_value = calculate_string_value(current_string)
# Compare this string with the best we've seen so far
if string_value < minimum_string_value:
minimum_string_value = string_value
best_string = current_string
return
index_to_replace = question_mark_indices[current_index]
for char in ['a', 'b', 'c']:
new_string = current_string[:index_to_replace] + char + current_string[index_to_replace + 1:]
generate_combinations(current_index + 1, new_string)
generate_combinations(0, input_string)
return best_string
def calculate_string_value(input_string):
string_value = 0
for i in range(len(input_string) - 1):
if input_string[i] != input_string[i+1]:
string_value += 1 # Punish different adjacent chars.
return string_valueWe want to fill in the question marks in the string to make it as small as possible alphabetically. We can accomplish this by examining each question mark and picking the smallest letter that doesn't create a duplicate with its immediate 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(input_string)
for index in range(string_length):
if string_as_list[index] == '?':
# Find the first available character
for char in 'abc':
is_valid = True
# Check the previous character if it exists
if index > 0 and string_as_list[index - 1] == char:
is_valid = False
# Check the next character if it exists
if index < string_length - 1 and string_as_list[index + 1] == char:
is_valid = False
if is_valid:
string_as_list[index] = char
break
return ''.join(string_as_list)
def replace_question_marks_in_string_to_minimize_its_value(input_string):
string_as_list = list(input_string)
string_length = len(input_string)
for index in range(string_length):
if string_as_list[index] == '?':
# Iterate through possible replacement chars 'a', 'b', 'c'
for replacement_char in 'abc':
valid_replacement = True
# Handle the case where we aren't at the beginning of the string
if index > 0 and string_as_list[index - 1] == replacement_char:
valid_replacement = False
# Handle the case where we aren't at the end of the string
if index < string_length - 1 and string_as_list[index + 1] == replacement_char:
valid_replacement = False
if valid_replacement:
# Assign replacement char and break the loop
string_as_list[index] = replacement_char
break
return ''.join(string_as_list)| Case | How to Handle |
|---|---|
| Null or empty input string | Return an empty string or throw an IllegalArgumentException, depending on requirements. |
| String consisting only of question marks | Iteratively replace with 'a', 'b', 'c' avoiding adjacent repetition. |
| String with a single character, which is a question mark | Replace the question mark with 'a'. |
| String with no question marks | Return the original string as is. |
| Question mark at the beginning of the string | Replace with a character different than the subsequent character. |
| Question mark at the end of the string | Replace with a character different than the preceding character. |
| Consecutive question marks in the string | Iteratively replace, ensuring each replacement differs from its neighbors. |
| Long string to consider performance implications | The linear time complexity of the iterative replacement ensures reasonable performance. |