Palindrome
excerpt: This covers the basic building blocks Two Pointers Moving Towards Each Other and Linear Scan using While Loop.
Write a function that tests whether a string is a palindrome.
Iterative Implementation
The two pointers are initialized to the beginning and the end of the string for comparison of characters inside a while loop.
|
|
One pointer can be used in combination with the relationship between the n and i to figure out the location of the right pointer without explicitly using a right pointer. In this case a for loop is used for traversing through the string.
|
|
Recursive Implementation
|
|
Compare the recursive version with the iterative version. The unit of work is done before the recursive call. The loop terminating condition is modified and becomes the base case.
Building Blocks
- Two Pointers Moving Towards Each Other
- Linear Scan using While Loop
- Unit of Work
- Terminating Condition in Loop and Base Case