<p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>

<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "aacecaaa"
<strong>Output:</strong> "aaacecaaa"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "abcd"
<strong>Output:</strong> "dcbabcd"
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>s</code> consists of lowercase English letters only.</li>
</ul>

class Solution {
public:
    int LPS(string s){
        int n=s.length();
        vector<int>lps(n,0);
        int i=1,len=0;
        while(i<n){
            if(s[i]==s[len]){
                len++;
                lps[i]=len;
                i++;
            }
            else{
                if(len>0){
                    len=lps[len-1];
                }
                else{
                    i++;
                }
            }
        }
        return lps[n-1];
    }
    string rev(string s){
        int n=s.length();
        int i=0,j=n-1;
        while(i<j and i<n){
            swap(s[i],s[j]);
            i++;
            j--;
        }
        return s;
    }
    string shortestPalindrome(string s) {
        string ns = s +"#" + rev(s);
        int lps=LPS(ns);
        // string ans="";
        // ans+=rev(s.substr(lps));
        // ans+=s.substr(0,lps);
        // ans+=s.substr(lps);
        // return ans;
        return rev(s.substr(lps))+s.substr(0,lps) + s.substr(lps);
    }
};
12500