Knowee
Questions
Features
Study Tools

Write a python program to count the number of palindrome strings that occurred in the given string.

Question

Write a python program to count the number of palindrome strings that occurred in the given string.

🧐 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
s = "madam arora teaches malayalam"
print(count_palindromes(s))

In this program, we define a function count_palindromes(s) that takes a string s as input. We initialize a counter count to 0. Then we iterate over all substrings of s using two nested loops. For each substring, we check if it is equal to its reverse (which means it's a palindrome), and if so, we increment the counter. Finally, we return the counter.

The test case "madam arora teaches malayalam" should return 18, as there are 18 palindromic substrings in this string.

This problem has been solved

Similar Questions

Palindrome CountWrite 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:2Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 madam2

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

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

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

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.