Solution: Valid Palindrome

Statement#

Write a function that takes a string, s, as an input and determines whether or not it is a palindrome.

Note: A palindrome is a word, phrase, or sequence of characters that reads the same backward as forward.

Constraints:

  • 11 \leq s.length 2×105\leq 2 \times 10^5
  • The string s will contain English uppercase and lowercase letters, digits, and spaces.

Solution#

So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.

Naive approach#

The naive approach to solve this problem is to reverse the string and then compare the reversed string with the original string. If they match, the original string is a valid palindrome. Although this solution has a linear time complexity, it requires extra space to store the reversed string, making it less efficient in terms of space complexity. Therefore, we can use an optimized approach to save extra space.

Optimized approach using two pointers#

A palindrome is a word or phrase that reads the same way when it is reversed. This means that the characters at both ends of the word or phrase should be exactly the same.

The two-pointers approach would allow us to solve this problem in linear time, without any additional space complexity or the use of built-in functions. This is because we’ll traverse the array from the start and the end simultaneously to reach the middle of the string.

Created with Fabric.js 3.6.6 A B C B A R L Check both ends of the string.They're both the same. Sample example 1

1 of 5

Created with Fabric.js 3.6.6 A B C B A R L Check the next two elements.They're both the same. Sample example 1

2 of 5

Created with Fabric.js 3.6.6 A B C B A R L We've reached the middle of thestring. Sample example 1

3 of 5

Created with Fabric.js 3.6.6 A B C B A R L It is a valid palindrome. Sample example 1

4 of 5

Created with Fabric.js 3.6.6 S T R A W R L Check both ends of the string.They are different. Therefore, this isnot a palindrome. Sample example 2

5 of 5

Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.

Step-by-step solution construction#

We’ll have two pointers, where the first pointer is at the starting element of our string, while the second pointer is at the end of the string. We move the two pointers towards the middle of the string and, at each iteration, we compare each element. The moment we encounter a nonidentical pair, we can return FALSE because our string can’t be a palindrome.

We will construct the solution step by step and the first step is to set up two pointers and move them toward the middle of the string. We can do that with the following code snippet:

Valid Palindrome

That's how we always traverse in opposite directions with the help of two pointers. The termination condition for our code is that the left pointer should always be less than the right pointer, because the moment they cross each other, we reach the middle of the string and don't need to go any further. We can check how this works with our example.

In the code sample above, we see that in the case of the palindromic strings, at each step in the traversal toward the middle of the string, the characters at both the left and the right indexes are identical. However, with the third test case, “TART” (which isn't a palindromic string), in the second iteration of the loop, we see that our output identifies that the characters at the left and right indexes aren't the same. This observation allows us to add a simple check to our code.

If we encounter a nonidentical pair, we can simply return FALSE, because the string isn't a palindrome, and we don't need to test any further. Otherwise, we're able to traverse to the middle of the string. In this case, we return TRUE, because each element has a match at the expected position in the string.

Valid Palindrome

Just the code#

Here’s the complete solution to this problem:

Valid Palindrome

Solution summary#

  • Initialize two pointers and move them from opposite ends.
  • The first pointer starts at the beginning of the string and moves toward the middle, while the second pointer starts at the end and moves toward the middle.
  • Compare the elements at each position to detect a nonmatching pair.
  • If both pointers reach the middle of the string without encountering a nonmatching pair, the string is a palindrome.

Time complexity#

The time complexity is O(n)O(n), where nn is the number of characters in the string. However, our algorithm will only run (n/2)(n/2) times, since two pointers are traversing toward each other.

Space complexity#

The space complexity is O(1)O(1), since we use constant space to store two indexes.

Valid Palindrome

Remove nth Node from End of List