DEV Community

Discussion on: Algorithm 101: 6 Ways to Check if a Word is a Palindrome

Collapse
 
chanting_baniya profile image
Aman • Edited

Good post. Really liked various ways you have noted. I will suggest one more. It should work too. Let me know...

Use two pointers starting from 0th and (string.length-1)th index, increase one and decrease the other respectively, till they meet at the center and compare characters at those pointers. If they match continue, else return false.

The advantage you gain is no excess space is required.

Collapse
 
leonardosnt profile image
Leonardo Santos • Edited

That is what I would do.
That approach is also useful to reverse arrays, just swap 0th <-> (len-1)th and so on.

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

I will definitely check it out. Sounds like divide-and-conquer. Lol