i found another mistake in my code that it only checks for the most inner two letters and if they are the same it return true other wise it return false without taking care of the other letters in the word
Heena Agarwal wrote:Thanks Stephen. I've actually listed similar steps in my first response. Just I got confused cause the calls were returning a boolean. Just in my steps, I'm passing the same char array and the position from the starting position as the argument ( it would also be the position from the length of the array ).
Mohamad Samy wrote:finally i found the solution for all even words as following:
S. Freeman wrote:When i think about recursion it always comes to mind as something that will save "space" for me. I am extremely lazy and i want to type as little code as possible.
S. Freeman wrote:When i think about recursion it always comes to mind as something that will save "space" for me.
Heena Agarwal wrote:Do you all also think that for this latter case also recursion is a better choice than iteration? I would have probably used simple iteration for this. But I know I might be wrong.
Heena Agarwal wrote:However with the palindrome problem (even if we ignore for the time being the long Strings ), even the complexity remained almost the same. It seemed like with every stack frame the String size reduced, but with every iteration also it would have reduced equally. So there was no added advantage of using recursion in the case of the palindrome problem.
Heena Agarwal wrote:Thank you, Winston. I shall keep these points in mind.
I remember being taught, not quite that many years ago (2009) that recursion was invented before iteration, but you can use (for example) Dijkstra's weakest precondition transformer semantics to prove that recursion and iteration are equivalent to each other.
Winston Gutkowski wrote: . . . I remember being taught (many years ago ) . . .
Campbell Ritchie wrote:I remember being taught...that recursion was invented before iteration, but you can use (for example) Dijkstra's weakest precondition transformer semantics to prove that recursion and iteration are equivalent to each other.
Stephan van Hulst wrote:Note that you *probably* don't have to worry about stack overflows if your recursive calls happen at an exit point of the method.
If you perform return isPalindrome(s.substring(1, s.length()-1)) the compiler is likely to pop the current frame off the stack before it pushes the next frame on. It's called tail-recursion, and I *think* that in javac it was implemented in 1.6.