Longest Chunked Palindrome Decomposition



Problem the palindrome formed by substring.Your task is to find the Longest Palindrome.



Example-1:


Input    : text = "abcxyzabc"
Output   : 3  
Explain  : [abc] [xyz] [abc] (These words are formed by Palindrome)


Example-2:


Input    : text = "abeatmnpqpqmneatab"
Output   : 8 
Explain  : [ab] [eat] [mn] [pq] [pq] [mn] [eat] [ab]   


Example-3:


Input    : text = "xyzaaxyz"
Output   : 4 
Explain  : [xyz] [a] [a] [xyz]   


Example-4:


Input    : text = "Apple"
Output   : 1 
Explain  : [Apple]   







Solution




public class Main
{
    public static void main(String [] args)
    {
        String text = "abcxyzabc";

        int count = 0;

        int len = 0;

        int l1 = 0, r1 = 1, l2 = text.length()-1, r2 = l2 + 1;

        while(r1&l;tr2)
        {
            if(text.substring(l1,r1).equals(text.substring(l2,r2)))
            {

                len = len + (r1-l1) + (r2-l2);

                count = count + 2;

                l1 = r1;

                r1 = l1 + 1;

                r2 = l2;

                l2 = r2-1;
            }

            else
            {
                r1++;

                l2--;
            }
        }

        if(len!=text.length()) count++;
        
        System.out.print(count);
    }
}




Output



3