Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to replace all question symbols to avoid consecutive repeating characters in Python
When working with strings containing question marks, we often need to replace them with letters to avoid consecutive repeating characters. This problem requires us to replace all '?' characters in a string with lowercase letters such that no two adjacent characters are the same.
So, if the input is like s = "hel??", then the output will be "helab". The first question mark can be anything except 'l', and the second one can be anything except 'a'.
Algorithm
To solve this problem, we will follow these steps ?
Handle the single character case first
Convert the string to a list for easy modification
Iterate through each character and replace '?' with an appropriate letter
For each '?', choose a letter that differs from adjacent characters
Return the modified string
Example
Let us see the following implementation to get better understanding ?
def solve(s):
if len(s) == 1:
if s == "?":
return "a"
return s
s = list(s)
for i in range(len(s)):
if s[i] == "?":
if i == 0 and s[i+1] == "?":
s[i] = "a"
elif i == 0 and s[i+1] == "a":
s[i] = "b"
elif i == 0:
s[i] = "a"
elif i == len(s)-1 and s[i-1] == "a":
s[i] = "b"
elif i == len(s)-1:
s[i] = "a"
elif s[i-1] == "a" and s[i+1] == "?":
s[i] = "b"
elif s[i+1] == "?":
s[i] = "a"
elif (s[i-1] == "a" and s[i+1] == "b") or (s[i-1] == "b" and s[i+1] == "a"):
s[i] = "c"
elif "a" in (s[i-1], s[i+1]):
s[i] = "b"
else:
s[i] = "a"
return "".join(s)
# Test the function
s = "hel??"
print(solve(s))
helab
How It Works
The algorithm processes each '?' character by checking its position and adjacent characters:
First position: Choose 'a' unless the next character is 'a', then choose 'b'
Last position: Choose 'a' unless the previous character is 'a', then choose 'b'
Middle positions: Choose a character that differs from both neighbors
Strategy: Use 'a', 'b', or 'c' as needed to avoid consecutive duplicates
Another Example
Let's test with a more complex case ?
def solve(s):
if len(s) == 1:
if s == "?":
return "a"
return s
s = list(s)
for i in range(len(s)):
if s[i] == "?":
if i == 0 and s[i+1] == "?":
s[i] = "a"
elif i == 0 and s[i+1] == "a":
s[i] = "b"
elif i == 0:
s[i] = "a"
elif i == len(s)-1 and s[i-1] == "a":
s[i] = "b"
elif i == len(s)-1:
s[i] = "a"
elif s[i-1] == "a" and s[i+1] == "?":
s[i] = "b"
elif s[i+1] == "?":
s[i] = "a"
elif (s[i-1] == "a" and s[i+1] == "b") or (s[i-1] == "b" and s[i+1] == "a"):
s[i] = "c"
elif "a" in (s[i-1], s[i+1]):
s[i] = "b"
else:
s[i] = "a"
return "".join(s)
# Test with different inputs
test_cases = ["hel??", "a?b?c", "???", "?"]
for test in test_cases:
result = solve(test)
print(f"Input: {test} ? Output: {result}")
Input: hel?? ? Output: helab Input: a?b?c ? Output: acbac Input: ??? ? Output: aba Input: ? ? Output: a
Conclusion
This algorithm efficiently replaces question marks with letters by checking adjacent characters and choosing the first available option from 'a', 'b', 'c'. The solution ensures no consecutive repeating characters while maintaining optimal time complexity of O(n).
