Count Palindrome Sub-Strings of a String | Practice | GeeksforGeeks

Given a string, the task is to count all palindromic sub-strings present in it. Length of palindrome sub-string must be greater than or equal to 2. 
Example
Input
N = 5
str = "abaab"
Output
3
Explanation:
All palindrome substring are : "aba" , "aa" , "baab"
Example
Input
N = 7
str = "abbaeae"
Output
4
Explanation:
All palindrome substring are : "bb" , "abba" ,"aea",
"eae"
Expected Time Complexity : O(|S|2)
Expected Auxilliary Space : O(|S|2)
Constraints:
2<=|S|<=500
0
rohit250820001 week ago
int CountPS(char S[], int N)
{
    int dp[N][N]; 
    memset(dp, 0, sizeof(dp));   
    int count = 0; 
    for(int g = 0; g < N; ++g) {
        for(int i = 0, j = g; j < N; ++i,++j) {
            
            int curr_length = j - i + 1;  
            if(curr_length == 1) {
                dp[i][j] = 1; 
            }
            else if(curr_length == 2 && S[i] == S[j]) {
                dp[i][j] = 1; 
            }else if(dp[i+1][j-1] && S[i] == S[j]) {
                dp[i][j] = 1; 
            }
            if(dp[i][j]) {
                count++; 
            }
        }
    }
    return count - N; 
}
0
gunjangoyal2821 month ago
int CountPS(char S[], int N)
{
    //code here
    int dp[N][N], ctr = 0;
    memset(dp,0,sizeof(dp));
    for(int gap = 0; gap < N; gap++){
        for(int i=0, j = gap; j<N; i++, j++){
            if(gap == 0){
                dp[i][j] = 1;
            }
            else if(gap == 1){
                if(S[i] == S[j]){
                    dp[i][j] = 1;
                }
                else{
                    dp[i][j] = 0;
                }
            }
            else if(S[i] == S[j] and dp[i+1][j-1] == 1){
                dp[i][j] = 1;
            }
            else{
                dp[i][j] = 0;
            } 
            if(dp[i][j]){
                ctr++;
            }
        }
    }
    return ctr-N;
}
0
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION
class Solution{     static boolean isPalindrome(String s)   {       if(s.length()>1)       {       int i=0;       int j=s.length()-1;       while(i<j)       {           if(s.charAt(i)!=s.charAt(j))           {               return false;           }           i++;           j--;       }       return true;       }       else       {           return false;       }   }   public int CountPS(String S, int N)   {       int count=0;       for(int i=0;i<N;i++)       {           for(int j=i+1;j<=N;j++)           {               if(isPalindrome(S.substring(i,j)))               {                   count++;               }           }       }          return count;   }}
0
mashhadihossain1 month ago
SIMPLE JAVA SOLTUION
class Solution{     static boolean isPalindrome(String s)   {       if(s.length()>1)       {       int i=0;       int j=s.length()-1;       while(i<j)       {           if(s.charAt(i)!=s.charAt(j))           {               return false;           }           i++;           j--;       }       return true;       }       else       {           return false;       }   }   public int CountPS(String S, int N)   {       ArrayList<String> list=new ArrayList<String>();       for(int i=0;i<N;i++)       {           for(int j=i+1;j<=N;j++)           {               if(isPalindrome(S.substring(i,j)))               {                   list.add(S.substring(i,j));               }           }       }          return list.size();   }}
+1
neeramrutia2 months ago
int count = 0;
       for(int i=0;i<N;i++) {
           count =count+ check(S,i,i);
           count =count+ check(S,i,i+1);
       }
       return count;
   }
   public int check(String S,int i,int j) {
       int count = 0;
       while(i>=0&&j<S.length()&&S.charAt(i)==S.charAt(j)) {
           if(j-i>=1) 
           count++;
           i--;
           j++;
           
       }
       
       return count;
+1
neeramrutia2 months ago
int i,j,count=0;       for(i=0;i<=N-2;i++)       {           for(j=i+2;j<=N;j++)           {               String s1=S.substring(i,j);               StringBuffer s2=new StringBuffer(s1);               s2.reverse();               String s3=s2.toString();               if(s1.equals(s3))               {                   count++;               }           }       }       return count;
0
ankitparashxr3 months ago
java
public static boolean check(String str)   {       if(str.length()<2)       {           return false;       }       int i =0;       int j =str.length()-1;       while(i<j)       {           if(str.charAt(i)!=str.charAt(j))           {               return false;           }           i++;           j--;       }       return true;   }   public int CountPS(String s, int n)   {       int count = 0;      for(int i =0;i<s.length();i++)      {          for(int j =i;j<s.length();j++)          {              if(check(s.substring(i,j+1)))              {                  count++;              }          }      }      return count;
+3
shashikantsolanki0423 months ago
int CountPS(char S[], int N)
{
    //code here
    int dp[N][N], ctr = 0;
    memset(dp,0,sizeof(dp));
    for(int gap=0; gap<N; gap++){
        for(int i=0,j=gap; j<N; i++,j++){
            if(gap == 0){
                dp[i][j] = 1;
            }
            else if(gap == 1){
                if(S[i] == S[j]){
                    dp[i][j] = 1;
                    ctr+=1;
                }
                else{
                    dp[i][j] = 0;
                }
            }
            else{
                if(S[i] == S[j] and dp[i+1][j-1] == 1){
                    dp[i][j] = 1;
                    ctr+=1;
                }
                else{
                    dp[i][j] = 0;
                }
            }
        }
    }
    return ctr;
}
0
gurucharanchouhan173 months ago
Simple Solution in java : 
 
class Solution{   public int CountPS(String s, int N)   {         int c=0;          for(int i=0;i<s.length();i++)          {             for(int j=i;j<s.length();j++)             {                 String g=s.substring(i,j+1);                                   if(g.length()>=2)                  {                     if(check(g))                       {                          c++;                       }                       else                       {                           continue;                       }                  }                  else                  {                      continue;                  }                        }          }       return c;   }       public static boolean check(String s)         {                int i=0;                int j=s.length()-1;
                while(i<=j)                {                     char ch=s.charAt(i);                     char lc=s.charAt(j);                    if(ch!=lc)                    {                       return false;                    }                    i++;                    j--;                }               return true;   } }
+1
vg713 months ago
int CountPS(char S[], int N){       int count=0;   //code here        for(int i=0;i<N;i++){       int mid=i;       int left=i-1;       int right=i+1;             while(left>=0 and right<N ){           if(S[left]==S[right]){               count++;               left--;               right++;           }           else{               break;           }       }   }       for(int i=0;i<N;i++){       int mid1=i;       int mid2=i+1;        if(S[mid1]==S[mid2]){           count++;          // cout<<mid1<<" "<<mid2<<endl;                  }       int left=mid1-1;       int right=mid2+1;             while(left>=0 and right<N and S[mid1]==S[mid2]  ){           if(S[left]==S[right]){               count++;              // cout<<left<<" "<<right<<endl;               left--;               right++;           }           else{               break;           }       }   }   return count;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.