Class Solution

java.lang.Object
g1301_1400.s1392_longest_happy_prefix.Solution

public class Solution extends Object
1392 - Longest Happy Prefix.<p>Hard</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;level&rdquo;</p> <p><strong>Output:</strong> &ldquo;l&rdquo;</p> <p><strong>Explanation:</strong> s contains 4 prefix excluding itself (&ldquo;l&rdquo;, &ldquo;le&rdquo;, &ldquo;lev&rdquo;, &ldquo;leve&rdquo;), and suffix (&ldquo;l&rdquo;, &ldquo;el&rdquo;, &ldquo;vel&rdquo;, &ldquo;evel&rdquo;). The largest prefix which is also suffix is given by &ldquo;l&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;ababab&rdquo;</p> <p><strong>Output:</strong> &ldquo;abab&rdquo;</p> <p><strong>Explanation:</strong> &ldquo;abab&rdquo; is the largest prefix which is also suffix. They can overlap in the original string.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10<sup>5</sup></code></li> <li><code>s</code> contains only lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details