Easy to understand c++ solution (6ms)


  • 0
    L
    class Solution {
    public:
        string longestPalindrome(string s) {
            int sLen=s.length();
            int leftMost=0,rightMost=0;
            int Start=0,End=0;
            int i=0;
            while(i<sLen){
                Start=i;
                do{
                    i++;
                }while(i<sLen&&s[i]==s[Start]);
                End=i-1;
                while(Start-1>=0&&End+1<sLen&&s[Start-1]==s[End+1]){
                    Start--;
                    End++;
                }
                if(rightMost-leftMost<End-Start){
                    leftMost=Start;
                    rightMost=End;
                }
            }
            return s.substr(leftMost,rightMost-leftMost+1);
        }
    };
    

Log in to reply
 

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