将给出字符串的问号转换成字符小写字母,使其满足回文要求

请尝试在评论区里写下答案(如不能清楚表述,那么你可能没真正理解)。欢迎参与,为下一次求职做准备。

Write a function solution that,given a string S of length N,returns any palindrome which can be obtained by replacing all of
the question marks in S by lowercase letters (‘a’-‘z).If no
palindrome can be obtained,the function should return the string
“NO”.
A palindrome is a string that reads the same both forwards and
backwards.Some examples of palindromes are:”kayak”,”radar”,
“mom”.
Examples:
1.Given S=”?ab??a”,the function should return “aabbaa”.
2.Given S=”bab??a”,the function should return “NO”.
3.Given S=”?a?”,the function may return “aaa”.It may also
return “zaz”,among other possible answers.
Tes
Assume that:
·N is an integer within the range[1.1,000];
·string S consists only of lowercases letters(‘a’-
‘z)or’?.

讨论数量: 3

这是特斯拉面试题,我挂在这道题上了;发给小伙伴看看,如何大家之后碰到希望可以解决

1年前 评论
S = input("please input the string:")
String_List = list(S)
n = len(String_List)
if n == 0:
    print("NO")

mid = int(n/2)

for i in range(0, mid):
    if String_List[i] == String_List[n-1-i] or String_List[i] == "?" or String_List[n-1-i] == "?":
        continue
    else:
        print("NO")

for m in range(0, n):
    if String_List[m] == "?":
        if String_List[n-1-m] == "?":
            String_List[m] = String_List[n-1-m] = 'z'
        else:
            String_List[m] = String_List[n-1-m]

print(''.join(String_List))

写的有点丑陋...我可能还没测全... 懒得改了 先午睡

1年前 评论

这里人很少啊,只有一个题?

4个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!