![]() |
VOOZH | about |
Given a string consisting of 'a', 'b', 'c', or '?', you are required to replace the question marks '?' in the string. The goal is to find a replacement that adheres to the following conditions:
Examples:
Input: S = "aba?ca?"
Output: "ababcab"
Explanation: first '?' was replaced by 'b' as 'a' and 'c' are adjacent to that , and last '?' can be replaced by 'b' and 'c' but 'b' comes first lexicographically.Input: S = "ba?"
Output: "bac"
Explanation: '?' was replaced by 'c' as 'a' is adjacent and 'b' is the starting of the string and '?' is the ending of the string.
Approach: The basic way to solve the problem is as follows:
Below is the implementation of the above idea.
ababcab
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1).