package LeetCode_1392 /** * 1392. Longest Happy Prefix * https://leetcode.com/problems/longest-happy-prefix/ * * 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 ...
1classSolution {2func longestDupSubstring(_ S: String) ->String {3varsa:[Int] = suffixArray(Array(S),26)4let n:Int =S.count5varlcp:[Int] =buildLCP(Array(S), sa)6varisa:[Int] = [Int](repeating:0,count:n)7foriin0..<n {isa[sa[i]] =i}8varmax:Int =09vararg:Int = -110...
Example 2: Input: s = "ababab" Output: "abab" Explanation: "abab" is the largest prefix which is also suffix. They can overlap in the original string. Example 3: Input: s = "leetcodeleet" Output: "leet" Example 4: Input: s = "a" Output: "" Constraints: 1 <= s.length <= ...
* s.substring(loc-ml[loc-1],loc) of length ml[loc-1] has fully matched prefix, * we are trying to extend this substring by one more letter, which is at loc*/intloc =i;while(loc >= 1) {//ml[loc - 1] gives how many prefix we have matched, so the next index to compare/ma...