Count All Palindrome Sub-Strings in a String | Set 2
Given a string, the task is to count all palindrome substring in a given string. Length of palindrome substring 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 dynamic programming based solution in below post. Count All Palindrome Sub-Strings in a String | Set 1 The solution discussed here is extension of Longest Palindromic Substring problem.
The idea is for each character in the given input string, we consider it as midpoint of a palindrome and expand it in both directions to find all palindromes of even and odd lengths. We use hashmap to keep track of all the distinct palindromes of length greater than 1 and return map size which have count of all possible palindrome substrings.
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
int countPalindromes(string s)
{
unordered_map<string, int > m;
for ( int i = 0; i < s.length(); i++) {
for ( int j = 0; j <= i; j++) {
if (!s[i + j])
break ;
if (s[i - j] == s[i + j]) {
if ((i + j + 1) - (i - j) > 1)
m[s.substr(i - j,
(i + j + 1) - (i - j))]++;
} else
break ;
}
for ( int j = 0; j <= i; j++) {
if (!s[i + j + 1])
break ;
if (s[i - j] == s[i + j + 1]) {
if ((i + j + 2) - (i - j) > 1)
m[s.substr(i - j,
(i + j + 2) - (i - j))]++;
} else
break ;
}
}
return m.size();
}
int main()
{
string s = "abbaeae" ;
cout << countPalindromes(s) << endl;
return 0;
}
|
Java
public class PalindromeSubstring {
static int countPS(String str){
String temp = "" ;
StringBuffer stf;
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
for ( int j = i + 1 ; j <= str.length(); j++) {
temp = str.substring(i, j);
if (temp.length() >= 2 ) {
stf = new StringBuffer(temp);
stf.reverse();
if (stf.toString().compareTo(temp) == 0 )
count++;
}
}
}
return count;
}
public static void main(String args[]) throws Exception {
String str = "abbaeae" ;
System.out.println(countPS(str));
}
}
|
Python3
def countPalindromes(s):
m = {}
for i in range ( len (s)):
for j in range (i + 1 ):
if (i + j > = len (s)):
break
if (s[i - j] = = s[i + j]):
if ((i + j + 1 ) - (i - j) > 1 ):
if (s[i - j:(i + j + 1 )] in m):
m[s[i - j:(i + j + 1 )]] + = 1
else :
m[s[i - j:(i + j + 1 )]] = 1
else :
break
for j in range (i + 1 ):
if (i + j + 1 > = len (s)):
break
if (s[i - j] = = s[i + j + 1 ]):
if ((i + j + 2 ) - (i - j) > 1 ):
if (s[i - j:i + j + 2 ] in m):
m[s[i - j:(i + j + 2 )]] + = 1
else :
m[s[i - j:(i + j + 2 )]] = 1
else :
break
return len (m)
s = "abbaeae"
print (countPalindromes(s))
|
C#
using System;
class GFG
{
static int countPS(String str)
{
String temp = "" ;
String stf;
int count = 0;
for ( int i = 0; i < str.Length; i++)
{
for ( int j = i + 1;
j <= str.Length; j++)
{
temp = str.Substring(i, j-i);
if (temp.Length >= 2)
{
stf = temp;
stf = reverse(temp);
if (stf.ToString().CompareTo(temp) == 0)
count++;
}
}
}
return count;
}
static String reverse(String input)
{
char [] a = input.ToCharArray();
int l, r = 0;
r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
public static void Main(String []args)
{
String str = "abbaeae" ;
Console.WriteLine(countPS(str));
}
}
|
Javascript
<script>
function countPalindromes(s)
{
let m = new Map();
for (let i = 0; i < s.length; i++) {
for (let j = 0; j <= i; j++) {
if (!s[i + j])
break ;
if (s[i - j] == s[i + j]) {
if ((i + j + 1) - (i - j) > 1){
if (m.has(s.substr(i - j,(i + j + 1) - (i - j)))){
m.set(s.substr(i - j,(i + j + 1) - (i - j)),m.get(s.substr(i - j,(i + j + 1) - (i - j))) + 1)
}
else m.set(s.substr(i - j,(i + j + 1) - (i - j)),1)
}
} else
break ;
}
for (let j = 0; j <= i; j++) {
if (!s[i + j + 1])
break ;
if (s[i - j] == s[i + j + 1]) {
if ((i + j + 2) - (i - j) > 1){
if (m.has(s.substr(i - j,(i + j + 2) - (i - j)))){
m.set(s.substr(i - j,(i + j + 2) - (i - j)),m.get(s.substr(i - j,(i + j + 2) - (i - j))) + 1)
}
else m.set(s.substr(i - j,(i + j + 2) - (i - j)),1)
}
} else
break ;
}
}
return m.size;
}
let s = "abbaeae"
document.write(countPalindromes(s), "</br>" )
</script>
|
Time complexity O(n2) Auxiliary Space O(n)
In the Approach discussed above we are using a unordered map which is taking O(n) space but we can do the same thing without using any extra space.Instead of making a hashmap we can directly count the palindromic substring.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countPalindromes(string s)
{
int n = s.size();
int count = 0;
for ( int i = 0; i < s.size(); i++) {
int left = i - 1;
int right = i + 1;
while (left >= 0 and right < n) {
if (s[left] == s[right])
count++;
else
break ;
left--;
right++;
}
}
for ( int i = 0; i < s.size(); i++) {
int left = i;
int right = i + 1;
while (left >= 0 and right < n) {
if (s[left] == s[right])
count++;
else
break ;
left--;
right++;
}
}
return count;
}
int main()
{
string s = "abbaeae" ;
cout << countPalindromes(s) << endl;
return 0;
}
|
Java
class GFG {
public static int countPalindromes(String s){
int count = 0 ;
for ( int i= 0 ; i<s.length(); i++){
count += expandFromCenter(s, i- 1 , i+ 1 ) + expandFromCenter(s, i , i+ 1 );
}
return count;
}
public static int expandFromCenter(String s, int left, int right){
int count = 0 ;
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){
count++;
left--;
right++;
}
return count;
}
public static void main (String[] args) {
String s = "abbaeae" ;
System.out.println(countPalindromes(s));
}
}
|
Python
def countPalindromes(s):
n = len (s)
count = 0
for i in range ( 0 , n):
left = i - 1
right = i + 1
while left > = 0 and right < n:
if s[left] = = s[right]:
count + = 1
else :
break
left - = 1
right + = 1
for i in range ( 0 , n):
left = i
right = i + 1
while left > = 0 and right < n:
if s[left] = = s[right]:
count + = 1
else :
break
left - = 1
right + = 1
return count
s = "abbaeae"
print (countPalindromes(s))
|
C#
using System;
public class GFG{
public static int countPalindromes( string s){
int count = 0;
for ( int i = 0; i < s.Length; i++)
{
count += expandFromCenter(s, i - 1, i + 1) + expandFromCenter(s, i , i + 1);
}
return count;
}
public static int expandFromCenter( string s, int left, int right){
int count = 0;
while (left >= 0 && right < s.Length && s[left] == s[right]){
count++;
left--;
right++;
}
return count;
}
public static void Main ( string [] args) {
string s = "abbaeae" ;
Console.WriteLine(countPalindromes(s));
}
}
|
Javascript
<script>
function countPalindromes(s)
{
n = s.length;
count = 0;
for (i = 0; i < s.length; i++) {
left = i - 1;
right = i + 1;
while (left >= 0 && right < n) {
if (s[left] == s[right])
count++;
else
break ;
left--;
right++;
}
}
for (i = 0; i < s.length; i++) {
left = i;
right = i + 1;
while (left >= 0 && right < n) {
if (s[left] == s[right])
count++;
else
break ;
left--;
right++;
}
}
return count;
}
s = "abbaeae" ;
document.write(countPalindromes(s));
</script>
|
Time complexity is O(n^2) in worst case because we are running one while loop inside a for loop because in while loop we are expanding to both directions so that’s why it’s O(n/2) in worst case when all characters are same eg : “aaaaaaaa” .
But we are not using any extra space , Space Complexity is O(1) .
Last Updated :
05 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...