Reputation: 834
I have been asked to give a context-free grammar that generates the following language (The alphabet is {0, 1}:
{ w| w is a palindrome }
To answer this correctly, I need to know if I can consider an empty string to be a palindrome. Thank you.
Upvotes: 3
Views: 7145
Reputation: 81
Actually, an empty string would be considered a palindrome since no matter how you look at it it will always be the same empty string backwards and forwards. Therefore, if you are trying to make a method called isPalindrome then your base case would be:
public static boolean isPalindrome(String text)
{
if(text.length()==1||text.length==0){
return true;}
}
//of course you would only need a base case if you were trying to implement the method using recursion.
Upvotes: 8
Reputation: 1161
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers.
According to this definition, an empty string would not qualify.
Upvotes: 0