Welcome to Subscribe On Youtube

1392. Longest Happy Prefix

Description

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:

  • 1 <= s.length <= 105
  • s contains only lowercase English letters.

Solutions

Solution 1: String Hashing

String Hashing is a method to map a string of any length to a non-negative integer, with the probability of collision being almost zero. String hashing is used to calculate the hash value of a string, which allows for quick determination of whether two strings are equal.

We choose a fixed value BASE, and consider the string as a number in BASE radix, assigning a value greater than 0 to represent each character. Generally, the values we assign are much smaller than BASE. For example, for strings composed of lowercase letters, we can assign a=1, b=2, …, z=26. We choose a fixed value MOD, and calculate the remainder of the BASE radix number divided by MOD, which is used as the hash value of the string.

Generally, we choose BASE=131 or BASE=13331, at which point the probability of hash value collision is extremely low. As long as the hash values of two strings are the same, we consider the two strings to be equal. Usually, MOD is chosen as $2^{64}$. In C++, we can directly use the unsigned long long type to store this hash value. When calculating, we do not handle arithmetic overflow. When overflow occurs, it is equivalent to automatically taking the modulus of $2^{64}$, which can avoid inefficient modulus operations.

Except for extremely specially constructed data, the above hash algorithm is unlikely to produce collisions. In general, the above hash algorithm can appear in the standard answers of the problem. We can also choose some appropriate values of BASE and MOD (such as large prime numbers), and perform several groups of hash operations. Only when the results are all the same do we consider the original strings to be equal, making it even more difficult to construct data that causes this hash to produce errors.

  • class Solution {
        private long[] p;
        private long[] h;
    
        public String longestPrefix(String s) {
            int base = 131;
            int n = s.length();
            p = new long[n + 10];
            h = new long[n + 10];
            p[0] = 1;
            for (int i = 0; i < n; ++i) {
                p[i + 1] = p[i] * base;
                h[i + 1] = h[i] * base + s.charAt(i);
            }
            for (int l = n - 1; l > 0; --l) {
                if (get(1, l) == get(n - l + 1, n)) {
                    return s.substring(0, l);
                }
            }
            return "";
        }
    
        private long get(int l, int r) {
            return h[r] - h[l - 1] * p[r - l + 1];
        }
    }
    
  • typedef unsigned long long ULL;
    
    class Solution {
    public:
        string longestPrefix(string s) {
            int base = 131;
            int n = s.size();
            ULL p[n + 10];
            ULL h[n + 10];
            p[0] = 1;
            h[0] = 0;
            for (int i = 0; i < n; ++i) {
                p[i + 1] = p[i] * base;
                h[i + 1] = h[i] * base + s[i];
            }
            for (int l = n - 1; l > 0; --l) {
                ULL prefix = h[l];
                ULL suffix = h[n] - h[n - l] * p[l];
                if (prefix == suffix) return s.substr(0, l);
            }
            return "";
        }
    };
    
  • class Solution:
        def longestPrefix(self, s: str) -> str:
            for i in range(1, len(s)):
                if s[:-i] == s[i:]:
                    return s[i:]
            return ''
    
    
  • func longestPrefix(s string) string {
    	base := 131
    	n := len(s)
    	p := make([]int, n+10)
    	h := make([]int, n+10)
    	p[0] = 1
    	for i, c := range s {
    		p[i+1] = p[i] * base
    		h[i+1] = h[i]*base + int(c)
    	}
    	for l := n - 1; l > 0; l-- {
    		prefix, suffix := h[l], h[n]-h[n-l]*p[l]
    		if prefix == suffix {
    			return s[:l]
    		}
    	}
    	return ""
    }
    
  • function longestPrefix(s: string): string {
        const n = s.length;
        for (let i = n - 1; i >= 0; i--) {
            if (s.slice(0, i) === s.slice(n - i, n)) {
                return s.slice(0, i);
            }
        }
        return '';
    }
    
    
  • impl Solution {
        pub fn longest_prefix(s: String) -> String {
            let n = s.len();
            for i in (0..n).rev() {
                if s[0..i] == s[n - i..n] {
                    return s[0..i].to_string();
                }
            }
            String::new()
        }
    }
    
    

All Problems

All Solutions