Clear c++ solution


  • 7
    V
    string longestPalindrome(string s) {
    	if (s.size() == 0 || s.size() == 1)
    		return s;
    	int start = 0, maxlen = 1;
        for(int i=0; i<s.size(); i++){
            int j=i, k=i;
            while(k<s.size()-1 && s[k] == s[k+1])
                k++;
            while(j>0 && k<s.size()-1 && s[j-1] == s[k+1]){
                k++;
                j--;
            }
            if(k-j+1 > maxlen){
                maxlen = k-j+1;
                start = j;
            }
        }
        return s.substr(start, maxlen);
    }

  • 0
    J

    no need to handle the situation when s.size() is 0 or 1. Just set maxlen = 0.


  • 0
    I

    What is the time complexity of the above algorithm ?


Log in to reply
 

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