Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 ?


What I have tried:

Python
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]
Posted
Updated 18-Aug-20 14:15pm

1 solution

Quote:
But it's hard to find as in above case. Any thoughts ?

Don't try to find a magic trick, for such a problem, go brute force.
Brute force is to try every possibilities.

Python
for any possible position of s2 in s1
  reset changes counter
  do necessary changes to have s2 in s1
  do necessary changes to make s1 a palindrome (without changing the place holding s2)
  if changes is better than previous tries, set the new minimum.

[update]
Quote:
How do you manage that 'any possible position of s2 in s1' ?

With input test="abcdefggfedcba" , sub_string="ifg"
Python
for any possible position of s2 in s1

gives
ifgdefggfedcba
aifgefggfedcba
abifgfggfedcba
abcifgggfedcba
abcdifggfedcba
abcdeifgfedcba
abcdefifgedcba
abcdefgifgdcba
abcdefggifgcba
abcdefggfifgba
abcdefggfeifga
abcdefggfedifg

Quote:
If length of sub_string is very long and then most of time consumed i checking what portion of sub_string exists. correct ? Any efficient way ? What could be the solution ?

There is no magic, if there is a better way, you need a good knowledge of the problem, and experiment different solutions and see what works best.
What is best solution for this
Python
test="abcfgfaba" , sub_string="ifg"
test="abcfgfaba" , sub_string="gfi"
 
Share this answer
 
v3
Comments
stackguru 19-Aug-20 1:30am    
@Patrice T : Thanks for responding. What would be the best approach ?
If length of sub_string is very long and then most of time consumed i checking what portion of sub_string exists. correct ? Any efficient way ? What could be the solution ?
stackguru 19-Aug-20 1:36am    
Above second example is my assumption, thinking beyond what's provided. May be I'm wrong also. Two examples were provided 1) test="abcdfg", sub_string="ab" , Operations would be 3, final string = abccba or abddba
2) test = "aaaaa", sub_string = "bbb" and the output has to be 3, because three changes are needed to make string abbba in this case.
stackguru 19-Aug-20 3:22am    
How do you manage that 'any possible position of s2 in s1' ?
If s2 = "abcd", possibilities are ['abcd','abc','bcd','ab','bc','cd'] . How to arrive at these possibilities ? Do we really need to bother about these cases ?
stackguru 19-Aug-20 3:23am    
putting up my assumptions. If you feel any other best solutions, I am happy to learn.
stackguru 19-Aug-20 5:18am    
I did not get concept of 'for any possible position of s2 in s1'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900