# Binary search: "banana", the answer must between 0 to 5, we can guess 3 at the first time.# We will check every possible substring with length 3 to see if we can find any duplicate.classSolution:deflongestDupSubstring(self,s:str) ->str:defhas_duplicate(length): seen =set()for i inrange(n - length +1): substring = s[i : i + length]if substring in seen:return substring seen.add(substring)return'' n =len(s) left, right =0, n longest_dup =''while left < right: mid = (left + right +1) //2 current_dup =has_duplicate(mid) longest_dup = current_dup or longest_dupif current_dup: left = midelse: right = mid -1return longest_dup