Simple C++ solution O(N)


#1

bool checkalphanumeric(char x){
if((x<‘a’+26&&x>=‘a’)||(x<‘A’+26&&x>=‘A’)||(x<‘0’+10&&x>=‘0’))
return true;
return false;
}
int Solution::isPalindrome(string A) {
int i=0;
int j=A.length()-1;
int flag=1;
while(i<j&&i<A.length()&&j>=0){
if(!checkalphanumeric(A[i])){
i++;
continue;
}
if(!checkalphanumeric(A[j])){
j–;
continue;
}
if(A[i]!=A[j]&&abs(A[i]-A[j])!=32){
flag=0;
break;
}
i++;
j–;
}
return flag;
}


#2

how about this

int Solution::isPalindrome(string A) {
    string temp;
    for(int i=0;i<A.size();i++){
        if(isalnum(A[i])){
            temp+=tolower(A[i]);
        }
    }
    int j=temp.length()-1;
    for(int i=0;i<temp.length()/2;i++){
        if(temp[i]!=temp[j-i]) return 0;
    }
    return 1;
}

#3

Space O(1)

int Solution::isPalindrome(string a) {
int beg=0,end=a.size()-1;
while(beg<end){
    while(beg<end and !((a[beg]>='a' and a[beg]<='z') or (a[beg]>='0' and a[beg]<='9') or (a[beg]>='A' and a[beg]<='Z'))){
        beg++;
    }
    while(beg<end and !((a[end]>='a' and a[end]<='z') or (a[end]>='0' and a[end]<='9') or (a[end]>='A' and a[end]<='Z'))){
        end--;
    }
    if(tolower(a[beg])==tolower(a[end])){
        beg++;end--;
    }else{
        return 0;
    }
}
return 1;

}


#4

Thank you very much @lalit-kumar_658 for sharing your code. It was very helpful for me.


#5

instead of temp[j-i] can i use j-- in for loop??