why TLE?


  • 0
    W

    It's just O(N) for "aaaaaaaaaaaaa...aaaaa" case, why do I get TLE?
    Thanks.

    """
    class Solution {
    public:
    string shortestPalindrome(string s) {
    if (s.empty()) {
    return "";
    }

        int i = s.size() - 1;
        for (; i >= 0; --i) {
            int start = 0, end = i;
            bool match = true;
            while (start < end) {
                if (s[start++] != s[end--]) {
                    match = false;
                    break;
                }
            }
            
            if (match) {
                break;
            }
        }
        
        ostringstream oss;
        for (int k = s.size() - 1; k >= i + 1; --k) {
            oss << s[k];
        }
        oss << s;
        return oss.str();
    }
    

    };
    """


Log in to reply
 

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