2023-01-08 22:40:13 +00:00
|
|
|
# Given a string s, return the longest prefix that is repeated somewhere else in the string.
|
|
|
|
# For example, "abcdabejf" would return "ab" as "ab" starts at the beginning of the string
|
|
|
|
# and is repeated again later. Do not use the find method.
|
|
|
|
|
|
|
|
def longestPrefixRepeated(s):
|
|
|
|
# Your code here...
|
|
|
|
longest = ""
|
2023-05-16 20:00:37 +00:00
|
|
|
|
|
|
|
for i in range(1, len(s) // 2 + 1):
|
2023-01-08 22:40:13 +00:00
|
|
|
if s[:i] in s[i:]:
|
|
|
|
longest = s[:i]
|
|
|
|
|
2023-05-16 20:00:37 +00:00
|
|
|
return longest
|