Monday 11 September 2017

Find all distinct palindromic sub-strings of a given string

Given a string of lowercase ASCII characters, find all distinct continuous palindromic sub-strings of it.
Examples:

Input: str = "abaaa"
Output:  Below are 5 palindrome sub-strings
a
aa
aaa
aba
b


Input: str = "geek"
Output:  Below are 4 palindrome sub-strings
e
ee
g
k
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test2
{
   public class PossiblePalindrome
    {
       public void FindPalindrome()
       {
           string str = Console.ReadLine();
           for (int i = 0; i < str.Length; i++)
           {
               string sta = "";
               for (int j = i; j < str.Length;j++ )
               {
                   sta = sta + str.Substring(j, 1);
                   CheckPalindrome(sta);
               }
                   
           }


           
       }
       public void CheckPalindrome(string str)
       { 
          
           int len ;
            if(str.Length==1)
               len=1;
           else
            len= str.Length/2;

           int j=str.Length;
           bool isP = true;
            for (int i = 0; i < len; i++)
            {
               
                if (str.Substring(i, 1) != str.Substring(j - 1, 1))
                {
                    isP = false;
                }

                j--;
            }

              if(isP)
               Console.WriteLine(str);
       }

    }
}


class Program
    {
        static void Main(string[] args)
        {

           //find possible palindrome of given string

            PossiblePalindrome objP = new PossiblePalindrome();
            objP.FindPalindrome();
}
}

No comments:

Post a Comment