Given a string str, the task is to count all the sub-strings of str which are palindromes and their length is prime.
Input: str = “geeksforgeeks”
“ee” and “ee” are the only valid sub-strings
Input: str = “abccc”
Approach: Using Sieve of Eratosthenes, find all the primes till the length of str because that is the maximum length a sub-string of str can have. Now starting from the smallest prime i.e. j = 2 till j ≤ len(str). If j is prime then count all the palindromic sub-strings of str whose length = j. Print the total count in the end.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a string can be split into even length palindromic substrings
- Count of Palindromic substrings in an Index range
- Count of K-size substrings having palindromic permutations
- Permutation of given string that maximizes count of Palindromic substrings
- Count of substrings of length K with exactly K distinct characters
- Count number of distinct substrings of a given length
- Count of non-palindromic strings of length M using given N characters
- Count of unique palindromic strings of length X from given string
- Count of odd length contiguous Palindromic sequences in a Matrix
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Lexicographically all Shortest Palindromic Substrings from a given string
- Rearrange the string to maximize the number of palindromic substrings
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Largest palindromic prime in an array
- Contiguous unique substrings with the given length L
- Number of palindromic subsequences of length k where k <= 3
- Sum of all odd length palindromic numbers within the range [L, R]
- Check if all the palindromic sub-strings are of odd length
- Minimize length of Substrings containing at least one common Character
- Convert all substrings of length 'k' from base 'b' to decimal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.