LC 125. Valid Palindrome

Nilanjan Deb · April 1, 2020

I solved this problem using a simple logic. Here I use the algorithm of remove duplicates without using extra space. First of all I just consider the alphanumeric characters and store it in current string. then check for palindrome.

This is my CPP solution.

class Solution {
public:
    bool isPalindrome(string s) {
        int n = s.size();
        int j = 0;
        for(int i=0;i<n;i++){
            if((s[i]>='A' && s[i]<='Z')||(s[i]>='a' && s[i]<='z')||(s[i]>='0'&&s[i]<='9')){
                if(s[i]>='A' && s[i]<='Z'){
                    s[j++]=s[i]-'A'+'a';
                }
                else{
                    s[j++]=s[i];
                }
            }
        }
        for(int i=0;i<j/2;i++){
            if(s[i]!=s[j-i-1]){
                return false;
            }
        }
        return true;
    }
};

Time Complexity O(n) Space Complexity O(1)



Dicussion Forum