Knowee
Questions
Features
Study Tools

Write a python program to count the number of palindrome strings that occurred in the given string. Input:The input consists of a string.Output:count number of palindrome stringsConstraints:1 < str < 10000Example:Input:madamOutput:2

Question

Write a python program to count the number of palindrome strings that occurred in the given string. Input:The input consists of a string.Output:count number of palindrome stringsConstraints:1 < str < 10000Example:Input:madamOutput:2

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python program that counts the number of palindrome strings in a given string:

def count_palindromes(s):
    count = 0
    for i in range(len(s)):
        for j in range(i+1, len(s)+1):
            if s[i:j] == s[i:j][::-1]:
                count += 1
    return count

# Test the function
print(count_palindromes('madam'))  # Output: 2

This program works by iterating over all possible substrings of the input string s. For each substring, it checks if the substring is the same as its reverse (which means it's a palindrome). If it is, it increments the count. Finally, it returns the count.

This problem has been solved

Similar Questions

Write a python program to check the given string is palindrome or not.

Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "bobseesanna" can be created by some ways:* bobseesanna = bob + sees + anna* bobseesanna = bob + s + ee + s + anna* bobseesanna = b + o + b + sees + a + n + n + a...We want to take the value of function CountPal(s) which is the number of different ways to use the palindromes to create the string s by the above method.InputThe string sOutputThe value of function CountPal(s), taking the modulo of 1 000 000 007 (109+7)Limitations0 < |s| <= 1000Sample 1:InputOutputbobseesanna18

Write a Python program to count the number of occurrences of a specific character in a string

Your friend is in university and there is a coding challenge going on. A string S of lowercase letters is displayed over the screen. The length of the string is N. An integer M is also displayed over the screen.Your task is write a program to determine if we can make the string Palindrome by performing at most M operations on the provided string

Given a string, find the minimum number of characters to be inserted to convert it to a palindrome.Examples :Input: str = "abcd"Output: 3Explanation: Inserted character marked with bold characters in dcbabcd, here we need minimum three characters to make it palindrome.

1/3

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.