293. Longest Happy Prefix (Length)

HardStringString

A happy prefix is a non-empty prefix of a string which is also a suffix (excluding the whole string itself). Given a string s, return the length of the longest happy prefix. Return 0 if none exists.

Input: A quoted string s.

Output: Integer — length of the longest happy prefix.

Examples

Example 1
Input: "level"
Output: 1
Explanation: 'l' is both prefix and suffix.
Example 2
Input: "ababab"
Output: 4
Explanation: 'abab' is prefix and suffix.
Example 3
Input: "abc"
Output: 0
Explanation: No happy prefix.

Constraints

Asked by

AppleAmazonMicrosoftGoogleMetaBloomberg
Solve this problem in the editor →