<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:
    def shortestPalindrome(self, s: str) -> str:
        s = '#' + '#'.join(s) + '#'
        lps = [0] * len(s)

        center, center_right = -1, -1
        for i in range(len(s)):
            if i < center_right:
                lps[i] = min(lps[center*2 - i], center_right - i)

            while 0 <= i-lps[i]-1 and i+lps[i]+1 < len(s) \
                    and s[i-lps[i]-1] == s[i+lps[i]+1]:
                lps[i] += 1

            if i + lps[i] > center_right:
                center, center_right = i, i + lps[i]

        for i in range(len(s) - 1, -1, -1):
            if i - lps[i] == 0:
                return s[i+lps[i]+1:].replace('#', '')[::-1] + s.replace('#', '')
        return ''
        
        # t = s + '#' + s[::-1]

        # def build_z_func(text: str) -> List[int]:
        #     z_func = [0] * len(text)
        #     z_func[0] = len(text)

        #     l, r = 0, 0
        #     for i in range(1, len(text)):
        #         if i < r:
        #             z_func[i] = min(z_func[i-l], r-i)
                
        #         while i+z_func[i] < len(text) and text[i+z_func[i]] == text[z_func[i]]:
        #             z_func[i] += 1
                
        #         if i + z_func[i] > r:
        #             l, r = i, i + z_func[i]
            
        #     return z_func
        

        # z_func = build_z_func(t)
        # for i in range(len(s) + 1, len(z_func)):
        #     if i + z_func[i] - 1 == len(z_func) - 1:
        #         return s[len(z_func) - i:][::-1] + s
        # return ''
19700