Back to photostream

Valid Palindrome Leetcode Solution

Problem Statement

Given a string, we have to determine if it is a palindrome, considering only alphanumeric characters i.e. numbers and alphabets only. We also have to ignore cases for alphabet characters.

Example

"A man, a plan, a canal: Panama"

true

Explanation:

 

"AmanaplanacanalPanama" is a valid palindrome.

"race a car"

false

Explanation:

 

"raceacar" is not a palindrome.

Naive Approach (Comparing with reverse)

To check if a string is palindrome or not we can simply reverse it and compare it with the original string. After reversing if it remains equal then the given string is a palindrome.

In this problem we have to ignore all the characters except alphabets and numbers. So for that we can filter the given string and save the filtered string in a new variable by removing all unwanted characters. Lets take an example:

 

 

 

 

 

 

 

 

 

We can see filtered string and reversed filtered string is not equal, hence it is not a valid palindrome.

Implementation for Valid Palindrome Leetcode Solution

C++ Program

#include

using namespace std;

 

bool isAlphaNum(char c)

{

if( (48

 

www.tutorialcup.com/leetcode-solutions/valid-palindrome-l...

46 views
0 faves
0 comments
Uploaded on October 14, 2021
Taken on December 29, 2020