<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>

class Solution {
public:
  string longestPrefix(string s) {
    //   ababab
    // ababab
    // _ _ _ _ _ _ _ _
    //         ^
    //       ^       ^
    int n = s.length();
    if (n == 1)
      return "";

    int first_char = s[0];
    string suffix = s;
    int suffix_len = n - 1;

    for (int i = 1; i < n; ++i) {
      //string t = suffix.substr(i);
      //if (t == s.substr(0, suffix_len))
      if (memcmp(suffix.c_str() + i, s.c_str(), suffix_len) == 0)
        return suffix.substr(i);
      --suffix_len;
    }

    return "";
  }
};
// s="level"
// max_len=1, max_len_str='l'
// return 'l'
// 
// s="ababab"
// max_len=0, max_len_str=""
// pos=2, 3
// for each pos
//    pos=2
//    max_len = 4

17190