Computer Science, asked by prakharpuri1999, 7 months ago

Given an input string word, split the string into exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the first substring that still allows the remaining word to be split into 2 palindromes. Similarly, choose the smallest second palindromic substring that leaves a third palindromic substring. If there is no way to split the word into exactly three palindromic substrings, print "Impossible" (without quotes). Every character of the string needs to be consumed. Cases not allowed - After finding 3 palindromes using above instructions, if any character of the original string remains unconsumed. No character may be shared in forming 3 palindromes.

Answers

Answered by nandhakishorens369
32

Answer:

'''Program to do as you instructed '''

# ipal is a function to know weather s is a palindromic

def ipal(s):

l=len(s)

for x in range(l):

 if not(s[x]==s[l-x-1]):

  return False

return True

# Q is the function that does the magic

def Q(s):

l=len(s)

for i in range(2,l):

 if ipal(s[:i]):

  print(s[:i])

  k=s[i:]

  p=len(k)

  for j in range(1,p):

   if ipal(s[i-1:j+1])and ipal(s[j:l+1]):

    print(s[:i],s[i:j],s[j:])

                   return 1

print('Imposible')

return 0

o="rarlolradar"

# that was our input string  

Q(o)

# Out puts rar lol radar

Explanation:

s[begining:end] is used to do splitting string in python and are known as slicing ... Please mark as brainlyset

Similar questions