What is palindrome
Palindrome is a word, phrase, or sequence that reads the same backwards as forwards as shown in the below example.
Example 1 palindrome using reverse function
reverse is overkill because you don’t need to generate an extra string, you just need to query the existing one. The following example checks the first and last characters are the same, and then walks further inside the string checking the results each time. It returns as soon as
s is not a palindrome.
The problem with the
reverse approach is that it does all the work up front. It performs an expensive action on a string, then checks character by character until the strings are not equal and only then returns false if it is not a palindrome. If you are just comparing small strings all the time then this is fine, but if you want to defend yourself against bigger input then you should consider this algorithm.
Example 2 palindrome without using reverse