Q: Given an input string, find out the number of substrings for the given strings which are palindromes.
Solution Strategy : Brute Force
Language : Python
Enter string: madam
m
madam
a
ada
d
a
m
Number of substrings which are palindrome are 7
Solution Strategy : Brute Force
Language : Python
Code
mystr=raw_input("Enter string") length=len(mystr) count=0; for i in range(length+1): for j in range(i+1, length+1): substr=mystr[i:j] if substr==substr[::-1]: print substr count+=1 print "Number of substrings which are palindrome are ",count
Output
Enter string: madam
m
madam
a
ada
d
a
m
Number of substrings which are palindrome are 7
No comments:
Post a Comment