# LeetCode 125. Valid Palindrome

## Description

https://leetcode.com/problems/valid-palindrome/

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

```Input: "A man, a plan, a canal: Panama"
Output: true
```

Example 2:

```Input: "race a car"
Output: false```

## Explanation

A palindrome, and its reverse, are identical to each other.

## Python Solution

``````class Solution:
def isPalindrome(self, s: str) -> bool:
i = 0
j = len(s) - 1

s = s.lower()

left = ""
right = ""

for i in range(0, len(s)):
if s[i].isalpha() or s[i].isdigit():
left += s[i]

for j in range(len(s) - 1, -1, -1):
if s[j].isalpha() or s[j].isdigit():
right += s[j]

return left == right``````
• Time complexity: O(N).
• Space complexity: O(N).

## One Thought to “LeetCode 125. Valid Palindrome”

1. John Fraser says:

Hi! I’ve posted my solution below. Could you tell me why for some cases I get an index out of bounds exception?

class Solution {
public boolean isPalindrome(String s) {

char[] arr = s.toCharArray();
int end = s.length() – 1;
for(int start = 0; start = s.length()) {
return false;
}
start++;
}

while(isLetter(arr[end]) == false) {
if(end = ‘a’ && c = ‘A’ && c <= 'Z') {
return true;
}
return false;
}
}