Does anyone use recursion?


  • 0
    L

    I want to use recursion but always told time limit exceeded. Does my code have any fault?

    class Solution {
    

    public:
    bool isPalindrome(string s) {
    string ss = help(s);
    return isHelp(ss);
    }

    bool isHelp(string s) {
        if (s[0] != s[s.size() - 1]) {
            return false;
        }
        if (s.size() <= 1) {
            return true;
        }
        return isHelp(s.substr(1, s.size() - 2));
    }
    
    string help(string s) {
        string result;
        int temp = 'A' - 'a';
        for (auto ch : s) {
            if (isalpha(ch) || isdigit(ch)) {
                result.push_back(tolower(ch));
            }
        }
        return result;
    }
    

    };


Log in to reply
 

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