Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Valid Palindrome

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.


  • 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.



class Solution {
		bool isPalindrome(string s) {
			int n = s.size();
			if (n == 0)
				return true;
			int left = next(s, -1);
			int right = prev(s, n);
			for (int i = next(s, -1), j = prev(s, n); i < j; i = next(s, i), j = prev(s, j)) {
				if (!equals(s[i], s[j]))
					return false;
			return true;
		int next(const string &s, int i) {
			while (!isalnum(s[i])) i++;
			return i;
		int prev(const string &s, int j) {
			while (!isalnum(s[j])) j--;
			return j;
		bool equals(char a, char b) {
			a = tolower(a);
			b = tolower(b);
			return a == b;
You can’t perform that action at this time.