Given a string, discover all non-single letter palindrome substrings.
Question
Given a string, discover all non-single letter palindrome substrings.
Solution
Sure, here is a Python solution for your problem:
def find_palindromes_in_sub_string(input_string, j, k):
count = 0
while j >= 0 and k < len(input_string):
if input_string[j] != input_string[k]:
break
if k - j + 1 > 1:
count += 1
j -= 1
k += 1
return count
def palindrome_sub_strings(input_string):
count = 0
for i in range(len(input_string)):
# find odd length palindromes
count += find_palindromes_in_sub_string(input_string, i - 1, i + 1)
# find even length palindromes
count += find_palindromes_in_sub_string(input_string, i - 1, i)
return count
input_string = "abcba"
print("Total palindrome substrings: ", palindrome_sub_strings(input_string))
Here is a step-by-step explanation of the code:
-
We define a helper function
find_palindromes_in_sub_string(input_string, j, k)that finds all palindromes in the substring ofinput_stringthat starts at indexjand ends at indexk. It returns the count of such palindromes. -
In the main function
palindrome_sub_strings(input_string), we iterate over each character in the input string. -
For each character, we call
find_palindromes_in_sub_string(input_string, i - 1, i + 1)to find all odd length palindromes that have the current character at the center. -
We also call
find_palindromes_in_sub_string(input_string, i - 1, i)to find all even length palindromes that have the current character at the center. -
We add up the counts from steps 3 and 4 to get the total count of palindrome substrings.
-
Finally, we print the total count of palindrome substrings.
Similar Questions
Write a function to find the longest palindromic substring in a given string
Given a string s, return the longest palindromic substring in s. Example 1:Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer.
Write a python program to check the given string is palindrome or not.
In this 3 Palindrome, 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.Constraints:Constraints1 <= the length of input sting <= 1000InputFirst line contains the input string consisting of characters between [a-z].OutputPrint 3 substrings one on each line.Example:Example 1InputnayannamantenetOutputnayannamantenetExplanation:The original string can be split into 3 palindromes as mentioned in the output.However, if the input was nayanamantenet, then the answer would be “Impossible”.
Find the Longest Palindromic Substring Zoe loves palindromes and she wants to find the longest palindromic substring within a given string. Can you help her find it?Constraints:NAExample:Sample Input:cbbdSample Output:bbExplanation:The longest palindromic substring from the above example is bbPublic Test Cases:# INPUT EXPECTED OUTPUT1 cbbdbb
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.