125 Valid Palindrome – Easy
Problem:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome.
Note: Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
Thoughts:
This is just a very basic string manipulation problem. Iteration of String.
Solutions:
public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.equals("")){
return true;
}
s = s.toLowerCase();
s = s.replaceAll("[^A-Za-z0-9]","");
int i = 0;
int j = s.length() -1;
for (; i < j; i ++, j --){
if (s.charAt(i) != s.charAt(j)){
return false;
}
}
return true;
}//isPalindrome
}Updated: 11/28/2016 Alternative solution:
Last updated