<p>A string is called a <strong>happy prefix</strong> if is a <strong>non-empty</strong> prefix which is also a suffix (excluding itself).</p>

<p>Given a string <code>s</code>, return <em>the <strong>longest happy prefix</strong> of</em> <code>s</code>. Return an empty string <code>&quot;&quot;</code> if no such prefix exists.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;level&quot;
<strong>Output:</strong> &quot;l&quot;
<strong>Explanation:</strong> s contains 4 prefix excluding itself (&quot;l&quot;, &quot;le&quot;, &quot;lev&quot;, &quot;leve&quot;), and suffix (&quot;l&quot;, &quot;el&quot;, &quot;vel&quot;, &quot;evel&quot;). The largest prefix which is also suffix is given by &quot;l&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;ababab&quot;
<strong>Output:</strong> &quot;abab&quot;
<strong>Explanation:</strong> &quot;abab&quot; is the largest prefix which is also suffix. They can overlap in the original string.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s</code> contains only lowercase English letters.</li>
</ul>

#define ll long long int
class Solution {
public:
    auto z_algo(string str){
        ll len = str.length();
        ll l = 0, r = 0;
        vector<ll> z(len);

        for (ll i = 1; i < len; i++) {
            if (i > r) {
                l = r = i;
                while (r < len && str[r] == str[r - l]) {
                    r++;
                }
                z[i] = r - l;
                r--;
            } else {
                ll idx = i - l;
                if (i + z[idx] <= r) {
                    z[i] = z[idx];
                } else {
                    l = i;
                    while (r < len && str[r] == str[r - l]) {
                        r++;
                    }
                    z[i] = r - l;
                    r--;
                }
            }
        }
        return z;
    }

    string longestPrefix(string s) {
        // string tot = s + '$' + s;
        auto z = z_algo(s);
        int mx = 0;
        for (int i = 0; i < z.size(); i++) {
            if (z[i] == z.size() - i) {
                mx = z[i];
                break;
            }
        }
        if (mx == s.size()) return "";
        return s.substr(0,mx);
    }
};

26810