Easy C++ solution using KMP algorithm ( O(n) time complexity )


#1
int lps(string &s)
{
    int n=s.length(),i=0,j=1;
    vector<int>v(n);
    v[0]=0;
    while(j<n)
    {
        if(s[i]==s[j])
        {
            v[j]=i+1;
            i++;
            j++;
        }
        else
        {
            if(i==0)
            {
                v[j]=0;
                j++;
            }
            else
            {
                i=v[i-1];
            }
        }
    }
    return v[n-1];
}
int Solution::solve(string A) 
{
    string aux=A;
    reverse(aux.begin(),aux.end());
    A=aux+"$"+A;
    return aux.length()-lps(A);
}