Simple C++ solution (8ms, 13 lines)


  • 154
    H
    string longestPalindrome(string s) {
        if (s.empty()) return "";
        if (s.size() == 1) return s;
        int min_start = 0, max_len = 1;
        for (int i = 0; i < s.size();) {
          if (s.size() - i <= max_len / 2) break;
          int j = i, k = i;
          while (k < s.size()-1 && s[k+1] == s[k]) ++k; // Skip duplicate characters.
          i = k+1;
          while (k < s.size()-1 && j > 0 && s[k + 1] == s[j - 1]) { ++k; --j; } // Expand.
          int new_len = k - j + 1;
          if (new_len > max_len) { min_start = j; max_len = new_len; }
        }
        return s.substr(min_start, max_len);
    }

  • 0
    T

    this is the most wonderful way I had ever seen


  • 0
    C

    Can you tell me how you calaulate the time is 8ms?Thank you.


  • 0
    H

    When the solution is accepted, click the "More Details" button. You can see the runtime and its distribution.


  • 0
    C

    Thank you。。。


  • 0
    L
    This post is deleted!

  • 0
    N
    This post is deleted!

  • 0
    Z

    Could you please tell me how can we get the explicit Time Complexity of this method(algo) ..? Thank you .


  • 0
    V

    wonderful solution! Thanks~


  • 2
    Y

    I think it has the best time complexity is O(N), which the inputs look like "aaaaaaa"; and the worst time complexity is O(N*N), which "the case scenarios are the inputs with multiple palindromes overlapping each other", such as "ababababab".


  • 4
    I

    Wonderful solution! Thanks~

    I rewrite it with C and use pointers.
    runtime: 0 ms

    char* longestPalindrome(char* s) {
    char *go,*max_start,*left,*right,*re;
    int max_length,temp_length;
    char *s_end;
    s_end = s+strlen(s);
    if (strlen(s) == 0) return s;
    if (strlen(s) == 1) return s;
    for(go = s,max_length = 1;go<s_end;)
    {
        if (s_end - go <= max_length * sizeof(char) / 2) break;
        left = go,right = go;
        while(*right==*(right+1)) right++;
        go = right + 1;
        while(left<s_end && left>s && *(left-1) == *(right+1))
        {
            right++;
            left--;
        }
        temp_length = (right-left)/sizeof(char) + 1;
        if(temp_length > max_length) {
            max_start = left;
            max_length = temp_length;
        }
    }
    re =(char *) malloc(max_length*sizeof(char)+1);
    go = re;
    while(max_length)
    {
        *(go) = *(max_start);
        go++;
        max_start++;
        max_length--;
    }
    *go = '\0';
    return re;
    

    }


  • 0
    B
    This post is deleted!

  • 17

    This kind of "13 lines" is actually due to poor writing style. :(


  • 0
    T

    This is still an O(N^2) algorithm right?


  • 0
    H

    Yes. It just avoids the discussion of odd and even cases.


  • 1
    Z

    Brilliant!

    I rewrite it using JAVA, runtime: 340ms

    public String longestPalindrome(String s) {
        if (s.isEmpty()) return "";
        if (s.length() == 1) return s;
        int min_start = 0, max_len = 1;
        for (int i = 0; i < s.length(); ) {
            if (s.length() - i <= max_len / 2) break;
            int j = i, k = i;
            while (k < s.length() - 1 && s.charAt(k + 1) == s.charAt(k)) ++k; // Skip duplicate characters.
            i = k + 1;
            while (k < s.length() - 1 && j > 0 && s.charAt(k + 1) == s.charAt(j - 1)) {
                ++k;
                --j;
            } // Expand.
            int new_len = k - j + 1;
            if (new_len > max_len) {
                min_start = j;
                max_len = new_len;
            }
        }
        return s.substring(min_start, min_start + max_len);
    }

  • 0
    W

    Actually the "if (s.size() == 1) return s;" line is not necessary.


  • 0
    J

    Good! no need to deal with even/odd problem.


  • 0
    S

    What's the complexity of it?


  • 2
    G

    Can someone explain the algorithm? I am not able to get my head around what i, j, k are trying to keep a track of?


Log in to reply
 

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