Site icon Wander In Dev

Valid Palindrom (LeetCode #125)

Challenge Statement

Constraints

Example 1:

Input: s = "A man, a plan, a canal: Panama"

Output: True

Explanation: “amanaplanacanalpanama” is a palindrome.

Example 2:

Input: s = "race a car"

Output: False

Explanation: “raceacar” is not a palindrome.

Example 3:

Input: s = " "

Output: True

Explanation: s is an empty string “” after removing non-alphanumeric characters.

Since an empty string reads the same forward and backward, it is a palindrome.

Solution

Below is my solution and some test cases. This solution has a linear time complexity O(n) and a constant space complexity O(1), where n is the length of the input string.

Exit mobile version