LeetCode-in-Java

1392. Longest Happy Prefix

Hard

A string is called a happy prefix if is a non-empty prefix which is also a suffix (excluding itself).

Given a string s, return the longest happy prefix of s. Return an empty string "" if no such prefix exists.

Example 1:

Input: s = “level”

Output: “l”

Explanation: s contains 4 prefix excluding itself (“l”, “le”, “lev”, “leve”), and suffix (“l”, “el”, “vel”, “evel”). The largest prefix which is also suffix is given by “l”.

Example 2:

Input: s = “ababab”

Output: “abab”

Explanation: “abab” is the largest prefix which is also suffix. They can overlap in the original string.

Constraints:

Solution

public class Solution {
    public String longestPrefix(String s) {
        char[] c = s.toCharArray();
        int n = c.length;
        int[] a = new int[n];
        int max = 0;
        int i = 1;
        while (i < n) {
            if (c[max] == c[i]) {
                max++;
                a[i] = max;
                i++;
            } else {
                if (max > 0) {
                    max = a[max - 1];
                } else {
                    a[i] = 0;
                    i++;
                }
            }
        }
        return s.substring(0, a[n - 1]);
    }
}