3ms CPP


  • 0
    S
    class Solution {
    public:
        string shortestPalindrome(string s) {
    	int i = s.size()-1;
    	int j, k;
    	while (i >= 0)
    	{
    		j = i;
    		while (j-1>=0 && s[j] == s[j - 1]) j--;
    		k = j - 1;
    		while (j >= 0 && i < s.size() && s[j] == s[i])
    		{
    			j--;
    			i++;
    		}
    		if (j == -1)
    		{
    			break;
    		}
    		i=k;
    	}
    	string ss = "";
    	while (i < s.size())
    	{
    		ss.push_back(s[i]);
    		i++;
    
    	}
    	reverse(ss.begin(), ss.end());
    	return ss + s;
        }
    };

Log in to reply
 

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