Shortcut for chapter specific information

Saturday, October 22, 2011

How to use a stack to test a palindrome?

Come to think of it, it's actually very simple.  You just need to push the first half of the string into the stack and for the second half, if the element is the same as the top of the stack, you can pop it out from the stack.   If the stack is empty, this means you have a palindrome.   If there is any one element which you can't pop, then we are not looking at a palindrome. 

No comments:

Post a Comment