-
Notifications
You must be signed in to change notification settings - Fork 1
/
pol_with_x.py
33 lines (23 loc) · 857 Bytes
/
pol_with_x.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Given a string with "?", return a palindrome if possible. EX: ?ab?b returns babab
strings_to_test = ["?ab??b", "a?b?a", "???", "a?c?b", "a??a", "a?b?c", "??a??", "a??b", "a?b?b?a", "a?b?c?d"]
def checkpol(string) -> str:
answer_list = [i for i in string]
if len(answer_list) % 2 != 0:
return "Not a palindrome"
l, r = 0, len(answer_list) - 1
while l < r:
if answer_list[l] == "?" and answer_list[r] == "?":
answer_list[l] = "a"
answer_list[r] = "a"
elif answer_list[l] == "?":
answer_list[l] = answer_list[r]
elif answer_list[r] == "?":
answer_list[r] = answer_list[l]
l += 1
r -= 1
answer_string = ""
for i in answer_list:
answer_string += i
return answer_string
for i in strings_to_test:
print(checkpol(i))