Scatter palindrome python assignment expert
Answer to Question #256251 in Python for rasi6. Find out the number of palindrome strings in a given string - Link
Learn more about our help with Assignments: Python Palindrome - 3 This Program name is Palindrome - 2. Write a Python program to Palindrome - 3 The below link contains Palindrome - 3 question, explanation and test cases https://drive.google.com/file/d/1ZELb2KSvV36MM4kiF-dTFTC43CeUiFH9/view?usp=sharing We need exact output when the code was run
Learn more about our help with Assignments: Python Given a string, the task is to count all palindrome sub string in a given string. Length of palindrome sub string is greater than or equal to 2. Examples: Input : str = "abaab" Output: 3 Explanation : All palindrome substring are : "aba" , "aa" , "baab" Input : str = "abbaeae" Output: 4 Explanation : All palindrome substring are : "bb" , "abba" ,"aea","eae" We have discussed a similar problem below. The above problem can be recursively defined. Initial Values : i = 0, j = n-1; Given string 'str' CountPS(i, j) // If length of string is 2 then we // check both character are same or not If (j == i+1) return str[i] == str[j] // this condition shows that in recursion if i crosses // j then it will be a invalid substring or // if i==j that means only one character is remaining // and we require substring of length 2 //in both the conditions we need to return 0 Else if(i == j || i > j) return 0; Else If str[i..j] is PALINDROME // increment count by 1 and check for // rest palindromic substring (i, j-1), (i+1, j) // remove common palindrome substring (i+1, j-1) return countPS(i+1, j) + countPS(i, j-1) + 1 - countPS(i+1, j-1); Else // if NOT PALINDROME // We check for rest palindromic substrings (i, j-1) // and (i+1, j) // remove common palindrome substring (i+1 , j-1) return countPS(i+1, j) + countPS(i, j-1) - countPS(i+1 , j-1); If we draw recursion tree of above recursive solution, we can observe overlapping Subproblems. Since the problem has overlapping sub-problems, we can solve it efficiently using Dynamic Programming. Below is a Dynamic Programming based solution. C++
Java
Python3
C#
PHP
Javascript
Time complexity: O(n2) Method 2: This approach uses Top Down DP i.e memoized version of recursion. Recursive soln: 1. Here base condition comes out to be i>j if we hit this condition, return 1. 2. We check for each and every i and j, if the characters are equal, if that is not the case, return 0. 3. Call the is_palindrome function again with incremented i and decremented j. 4. Check this for all values of i and j by applying 2 for loops. Implementation: C++
Java
Python3
C#
Javascript
Count All Palindrome Sub-Strings in a String | Set 2 This article is contributed by Nishant_Singh(Pintu). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. |