-3

Given a string aaaabaabaaaa(s to be generic), how many possible palindrome of size > 2 (n to be generic) can be generated from this string?

I need to know how to count number of palindromes and to print those.

Eg. for above string aaa, aaaa, aba, aba, aaa, aaaa, abaaba, aaaabaabaaaa.

-Question asked in SAP(hiring for java) interview

TestUser
  • 33
  • 8

1 Answers1

0
 public void palindrome(String string)
 {
    char[] charArray = string.toCharArray();

    count = 0;
    i = 0;
    while(i < charArray.lenght)
    {
       if(charArray[i] == charArray[charArray.lenght - i - 1])
       {
       }
       else
       {
           System.out.println("not a palindrome");
           break;
       }
    }
}

this is the basic logic to test if it is a palindrome.

if the length of the string is even then "" is a palindrome as well or if it is odd the middle char is also a palindrome. So if((string.length()%2) == 0) then it is even or else it is an odd.

I will let you figure the rest out.

drhunn
  • 21
  • 1
  • 2
  • 13
  • hit you might want to take the original sting and make a charArray then rebuild the string one char at a time. You could call the method up above with each iteration of the loop and you will need to add a return statement to the method above. – drhunn Dec 26 '13 at 07:21