Как преобразовать строку в палиндром, при условии, что подстрока должна существовать в строке палиндрома ?
Given a String S1 and String S2. Convert string S1 to a palindrome string such S2 is a substring of that palindromic string. This conversion must be done by using minimum number pf operations. You are allowed to use only one operation in which you can replace any character of string S1 with any other character. his operation can be used any number of times. I have written normal code to make it as palindrome by replacing characters. I'm not getting how to write the logic to replace substring elements in string. 1) n = "aaaaa" and string (substring) m = "bbb" and the output has to be 3, because three changes are needed to make string abbba in this case. 2) test="abcdefggfedcba" , sub_string="ifg". sub_string not present in test. But portion of sub_string it exists test. So, here have to change just two characters ('e' from efg/gfe) that would be palindrome and ensuring sub_string in test as well. What's efficient way to handle it ? I see, str.find(sub_string). But it's hard to find as in above case. Any thoughts ?
Что я уже пробовал:
string1 = "rstutir" #string1 = list(string1) sub_string = "abc" if string1 == string1[::-1]: if sub_string in string1: print("Valid palindrome and sub string exists") else: # How to make sure sub_string exists in string # with less number of operations else: count = 0 for i in range(len(string1)//2): if(string1[i]== string1[n-i-1]): continue count += 1 if(string1[i]<string1[n-i-1]): string1[n-i-1]= string1[i] else: string1[i]= string1[n-i-1]