Valid Palindrome-Recursion

For the following problem Palindrome I have using Recursion to solve it.

The code for the problem is

class Solution {
public:
    
    bool checkp(string s,int l,int r){
        
        if(l>=r){
            return true;
        }
        
        if(s[l]!=s[r]){
            return false;
        }
        
       return checkp(s,l+1,r-1);
    }
    bool isPalindrome(string s) {
        
       string rm;
       for(int i=0;i<s.length();i++){
           
           if((s[i]>=65 && s[i]<=90)||(s[i]>=97 && s[i]<=122)){
               
               rm+=tolower(s[i]);
               
               
           }else if(s[i]>=48 && s[i]<=57){
               rm+=s[i];
           }
           
       }
        
        // cout<<rm<<"\n";
        
       return checkp(rm,0,rm.length()-1);
       
    }
};

Can anyone Please Explain Why I am Getting MLE.Not able to Understand.

The signature of the function checkp must be

bool checkp(string& s, int l, int r) // pass the reference

instead of

bool checkp(string s, int l, int r) // pass by value

Since passing the string as a value will make copies of the same string \cfrac{N}{2} times, where N = \text{length of string}.

That’s not MLE, it’s TLE - stands for Time Limit Exceeded.

2 Likes

Thanks !!But I got MLE (Memory Limit Exceeded ) in Leetcode for one last testcase.

Did you try submitting after modifying the code as I said?

1 Like

Haa It worked with the change

1 Like