Difference in condition in a loop condition


  • 0
    M

    Hi,

    I am not sure why my code is not accepted for this solution :

    But

        class Solution {
    public:
        bool isPalindrome(string str) {
            transform(str.begin(),str.end(),str.begin(),::tolower);
            string stripped;
            for(int i=0;i<str.length();i++){
                    if(isalnum(str[i])){
                            stripped.push_back(str[i]);
                    }
            }
            bool res = true;
            for(int i = 0,j = stripped.length()-1;i<str.length();i++,j--){ //CODE DIFFERENCE IN THIS LINE : NOTE THE CONDITION
    //              cout<<"Comparing "<<stripped[i]<<"and "<<stripped[j]<<endl;
                    if(stripped[i]!=stripped[j]){
                            return false;
                    }
            }
    //      cout<<stripped;
            return true;
    
        }
    };
    

    But is accepted for

    class Solution {
    public:
        bool isPalindrome(string str) {
            transform(str.begin(),str.end(),str.begin(),::tolower);
            string stripped;
            for(int i=0;i<str.length();i++){
                    if(isalnum(str[i])){
                            stripped.push_back(str[i]);
                    }
            }
            bool res = true;
            for(int i = 0,j = stripped.length()-1;j>=0;i++,j--){ //CODE DIFFERENCE IN THIS LINE : NOTE THE CONDITION
    //              cout<<"Comparing "<<stripped[i]<<"and "<<stripped[j]<<endl;
                    if(stripped[i]!=stripped[j]){
                            return false;
                    }
            }
    //      cout<<stripped;
            return true;
    
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.