Distinct palindromic sub-strings of the given string using Dynamic Programming
Given a string str of lowercase alphabets, the task is to find all distinct palindromic sub-strings of the given string.
Examples:
Input: str = “abaaa”
Output: 5
Palindromic sub-strings are “a”, “aa”, “aaa”, “aba” and “b”Input: str = “abcd”
Output: 4
Approach: The solution to this problem has been discussed here using Manacher’s algorithm. However we can also solve it using dynamic programming.
Create an array dp[][] where dp[i][j] is set to 1 if str[i…j] is a palindrome else 0. After the array has been generated, store all the palindromic sub-strings in a map in order to get the count of distinct sub-strings.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count // of distinct palindromic sub-strings // of the given string s int palindromeSubStrs(string s) { // To store the positions of // palindromic sub-strings int dp[s.size()][s.size()]; int st, end, i, j, len; // Map to store the sub-strings map<string, bool> m; for (i = 0; i < s.size(); i++) { // Sub-strings of length 1 are palindromes dp[i][i] = 1; // Store continuous palindromic sub-strings m[string(s.begin() + i, s.begin() + i + 1)] = 1; } // Store palindromes of size 2 for (i = 0; i < s.size() - 1; i++) { if (s[i] == s[i + 1]) { dp[i][i + 1] = 1; m[string(s.begin() + i, s.begin() + i + 2)] = 1; } // If str[i...(i+1)] is not a palindromic // then set dp[i][i + 1] = 0 else { dp[i][i + 1] = 0; } } // Find palindromic sub-strings of length>=3 for (len = 3; len <= s.size(); len++) { for (st = 0; st <= s.size() - len; st++) { // End of palindromic substring end = st + len - 1; // If s[start] == s[end] and // dp[start+1][end-1] is already palindrome // then s[start....end] is also a palindrome if (s[st] == s[end] && dp[st + 1][end - 1]) { // Set dp[start][end] = 1 dp[st][end] = 1; m[string(s.begin() + st, s.begin() + end + 1)] = 1; } // Not a palindrome else dp[st][end] = 0; } } // Return the count of distinct palindromes return m.size(); } // Driver code int main() { string s = "abaaa"; cout << palindromeSubStrs(s); return 0; } |
Python3
# Python3 implementation of the approach # import numpy lib as np import numpy as np; # Function to return the count # of distinct palindromic sub-strings # of the given string s def palindromeSubStrs(s) : # To store the positions of # palindromic sub-strings dp = np.zeros((len(s),len(s))); # Map to store the sub-strings m = {}; for i in range(len(s)) : # Sub-strings of length 1 are palindromes dp[i][i] = 1; # Store continuous palindromic sub-strings m[s[i: i + 1]] = 1; # Store palindromes of size 2 for i in range(len(s)- 1) : if (s[i] == s[i + 1]) : dp[i][i + 1] = 1; m[ s[i : i + 2]] = 1; # If str[i...(i+1)] is not a palindromic # then set dp[i][i + 1] = 0 else : dp[i][i + 1] = 0; # Find palindromic sub-strings of length>=3 for length in range(3,len(s) + 1) : for st in range(len(s) - length + 1) : # End of palindromic substring end = st + length - 1; # If s[start] == s[end] and # dp[start+1][end-1] is already palindrome # then s[start....end] is also a palindrome if (s[st] == s[end] and dp[st + 1][end - 1]) : # Set dp[start][end] = 1 dp[st][end] = 1; m[s[st : end + 1]] = 1; # Not a palindrome else : dp[st][end] = 0; # Return the count of distinct palindromes return len(m); # Driver code if __name__ == "__main__" : s = "abaaa"; print(palindromeSubStrs(s)); # This code is contributed by AnkitRai01 |
5
Recommended Posts:
- Find distinct characters in distinct substrings of a string
- Rearrange the string to maximize the number of palindromic substrings
- Minimum changes to a string to make all substrings distinct
- Find all distinct palindromic sub-strings of a given string
- Count of distinct substrings of a string using Suffix Array
- Count of distinct substrings of a string using Suffix Trie
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Count of Palindromic substrings in an Index range
- Count all Prime Length Palindromic Substrings
- Make palindromic string non-palindromic by rearranging its letters
- Count number of distinct substrings of a given length
- Count number of substrings with exactly k distinct characters
- Count distinct substrings that contain some characters at most k times
- Dynamic Programming on Trees | Set 2
- Dynamic Programming on Trees | Set-1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01



