Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
package beginnersbook.com;
import java.util.Scanner;
class PalindromeCheck
{
//My Method to check
public static boolean isPal(String s)
{ // if length is 0 or 1 then String is palindrome
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
/* check for first and last char of String:
* if they are same then do the same thing for a substring
* with first and last char removed. and carry on this
* until you string completes or condition fails
* Function calling itself: Recursion
*/
return isPal(s.substring(1, s.length()-1));

/* If program control reaches to this statement it means
* the String is not palindrome hence return false.
*/

What I have tried:

i want best program for this question with time and space complexity
Posted
Updated 29-Mar-18 13:10pm
Comments
Richard Deeming 29-Mar-18 13:33pm    
Your homework is set to test what you know, not how good you are at begging strangers on the internet to do your work for you.

Try it yourself, and you'll probably find it's not as hard as you think. After all, it will be based on topics you've recently covered in your course.

If you really don't know where to start, then talk to your teacher. That's what they're paid for!
CPallini 29-Mar-18 15:30pm    
The code looks fine. What's the problem?

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
I would try to replace
Java
return isPal(s.substring(1, s.length()-1));

with
Java
return isPal(s.substring(1, s.length()-2));

Quote:
i want best program for this question with time and space complexity

If not for learning purpose, recursion is a bad solution, a loop will be more efficient.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900